递归移动特定类型的文件并保留其目录结构
我有一个目录,其中包含多个带有 mov 和 jpg 文件的子目录。
/dir/
/subdir-a/ # contains a-1.jpg, a-2.jpg, a-1.mov
/subdir-b/ # contains b-1.mov
/subdir-c/ # contains c-1.jpg
/subdir-d/ # contains d-1.mov
... # more directories with the same pattern
我需要找到一种使用命令行工具(最好在 Mac OSX 上)将所有 mov 文件移动到新位置的方法。但是,一个要求是保持目录结构,即:
/dir/
/subdir-a/ # contains a-1.mov
/subdir-b/ # contains b-1.mov
# NOTE: subdir-c isn't copied because it doesn't have mov files
/subdir-d/ # contains d-1.mov
...
我熟悉 find
、grep 和
xargs
但不知道如何解决这个问题。预先非常感谢!
I have a directory which contains multiple sub-directories with mov and jpg files.
/dir/
/subdir-a/ # contains a-1.jpg, a-2.jpg, a-1.mov
/subdir-b/ # contains b-1.mov
/subdir-c/ # contains c-1.jpg
/subdir-d/ # contains d-1.mov
... # more directories with the same pattern
I need to find a way using command-line tools (on Mac OSX, ideally) to move all the mov files to a new location. However, one requirement is to keep directory structure i.e.:
/dir/
/subdir-a/ # contains a-1.mov
/subdir-b/ # contains b-1.mov
# NOTE: subdir-c isn't copied because it doesn't have mov files
/subdir-d/ # contains d-1.mov
...
I am familiar with find
, grep
, and xargs
but wasn't sure how to solve this issue. Thank you very much beforehand!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这稍微取决于您的操作系统,更具体地说,取决于您的
tar
版本中的功能以及您是否有命令cpio
。它还取决于您的文件名中是否有换行符(特别是);大多数人不这样做。选项#1
选项#2
cpio
命令具有传递(复制)模式,可以在给定文件列表的情况下完全执行您想要的操作名称,每行一个,在其标准输入上。某些版本的
tar
命令具有从标准输入读取文件名列表(每行一个)的选项;在 MacOS X 上,该选项是-T -
(其中单独的-
表示“标准输入”)。对于第一个tar
命令,选项-f -
表示(在使用-c
编写存档的上下文中,写入标准输出) ;在第二个tar
命令中,-x
选项表示-f -
表示“从标准输入读取”。可能还有其他选择;请仔细查看
tar
的手册页或帮助输出。此过程复制文件而不是移动它们。操作的后半部分将是:
It depends slightly on your O/S and, more particularly, on the facilities in your version of
tar
and whether you have the commandcpio
. It also depends a bit on whether you have newlines (in particular) in your file names; most people don't.Option #1
Option #2
The
cpio
command has a pass-through (copy) mode which does exactly what you want given a list of file names, one per line, on its standard input.Some versions of the
tar
command have an option to read the list of file names, one per line, from standard input; on MacOS X, that option is-T -
(where the lone-
means 'standard input'). For the firsttar
command, the option-f -
means (in the context of writing an archive with-c
, write to standard output); in the secondtar
command, the-x
option means that the-f -
means 'read from standard input'.There may be other options; look at the manual page or help output of
tar
rather carefully.This process copies the files rather than moving them. The second half of the operation would be:
ASSERT:没有文件包含换行符。然而,空格是可以的。
ASSERT: No files have newline characters in them. Spaces, however, are AOK.
作为大文件,如果它们位于同一文件系统上,您不想复制它们,而只是在移动时复制它们的目录结构。
您可以使用此功能:
或者,添加对合并现有目录的支持:
Being large files, if they are on the same file system you don't want to copy them, but just to replicate their directory structure while moving.
You can use this function:
Or, adding support to merging existing directories:
复制文件会更容易,例如:
It is easier to just copy the files like:
从“dir 的父目录执行此命令:
然后 cd 到要将文件移动到的目录并执行此命令:
from the parent directory of "dir execute this:
Then cd to the directory you want to move the files to and execute this:
如果您想将所有
mov
文件移动到名为新位置的目录,这应该可行 -但是,如果您希望移动
mov
文件及其子目录,那么您可以做这样的事情 -步骤 1:使用 cp 将 /dir 的整个结构复制到新位置
步骤 2:从 newdir 中查找
jpg
文件并将其删除。测试:
This should work if you want to move all
mov
files to a directory called new location -However, if you wish to move the
mov
files along with their sub-dirs then you can do something like this -Step 1: Copy entire structure of /dir to a new location using
cp
Step 2: Find
jpg
files from newdir and delete them.Test:
我修改了@djjeck的功能,因为它没有按照我的需要工作。下面的函数将源文件移动到目标目录,同时在源文件路径中创建所需的层次结构级别(请参见下面的示例):
示例:
I amended the function of @djjeck, because it didn't work as I needed. The function below moves a source file to a destination directory also creating the needed levels of hierarchy in the source file path (see the example below):
example: