bash 使用 mv 仅将文件从多个目录递归移动到一个目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19880246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:33:51  来源:igfitidea点击:

Move only files recursively from multiple directories into one directory with mv

bashunixgnudirectorymv

提问by user1067257

I currently have ~40k RAW images that are in a nested directory structure. (Some folders have as many as 100 subfolders filled with files.) I would like to move them all into one master directory, with no subfolders. How could this be accomplished using mv? I know the -rswitch will copy recursively, but this copies folders as well, and I do not wish to have subdirectories in the master folder.

我目前有大约 40k RAW 图像,它们位于嵌套目录结构中。(有些文件夹有多达 100 个装满文件的子文件夹。)我想将它们全部移动到一个主目录中,没有子文件夹。如何使用mv? 我知道-rswitch 会递归复制,但这也会复制文件夹,我不希望在主文件夹中有子目录。

回答by gniourf_gniourf

If your photos are in /path/to/photos/and its subdirectories, and you want to move then in /path/to/master/, and you want to select them by extension .jpg, .JPG, .png, .PNG, etc.:

如果你的照片是在/path/to/photos/和它的子目录,并希望在移动然后/path/to/master/,并且要通过扩展来选择它们.jpg.JPG.png.PNG,等:

find /path/to/photos \( -iname '*.jpg' -o -iname '*.png' \) -type f -exec mv -nv -t '/path/to/master' -- {} +

If you don't want to filter by extension, and just move everything (i.e., all the files):

如果您不想按扩展名过滤,只需移动所有内容(即所有文件):

find /path/to/photos -type f -exec mv -nv -t '/path/to/master' -- {} +

The -noption so as to not overwrite existing files (optional if you don't care) and -voption so that mvshows what it's doing (very optional).

-n不覆盖现有文件的选项(如果您不关心,则-v可选)和选项,以便mv显示它在做什么(非常可选)。

The -toption to mvis to specify the target directory, so that we can stack all the files to be moved at the end of the command (see the +delimiter of -exec). If your mvdoesn't support -t:

-t选项mv是指定目标目录,这样我们就可以堆叠中的所有文件,在命令结束(见要移动+的分隔符-exec)。如果您mv不支持-t

find /path/to/photos \( -iname '*.jpg' -o -iname '*.png' \) -type f -exec mv -nv -- {} '/path/to/master' \;

but this will be less efficient, as one instance of mvwill be created for each file.

但这会降低效率,因为mv将为每个文件创建一个实例。

Btw, this movesthe files, it doesn't copythem.

顺便说一句,这会移动文件,而不是复制它们。

Remarks.

评论。

  • The directory /path/to/mastermust already exist (it will not be created by this command).
  • Make sure the directory /path/to/masteris notin /path/to/photos. It would make the thing awkward!
  • 该目录/path/to/master必须已经存在(它不会被此命令创建)。
  • 确保目录/path/to/master是不是/path/to/photos。这会让事情变得尴尬!

回答by anubhava

Make use of -execdiroption of find:

使用以下-execdir选项find

find /path/of/images -type f -execdir mv '{}' /master-dir \;

As per man find:

根据man find

 -execdir utility [argument ...] ;
     The -execdir primary is identical to the -exec primary with the exception that 
     utility will be executed from the directory that holds the current
     file.  The filename substituted for the string ``{}'' is not qualified.

Since -execdirmakes findexecute given command from each directory therefore only base filename is moved without any parent path of the file.

由于-execdir使find从每个目录执行给定的命令,因此只移动基本文件名而没有文件的任何父路径。

回答by WhyteWolf

find <base location of files> -type -f -name \*\.raw -exec mv {} master \;

回答by Mika?l Mayer

If your hierachy is only one level deep, here is another way using the automated tools of StringSolver:

如果您的层次结构只有一层深,这是使用StringSolver自动化工具的另一种方法:

mv -a firstfolder/firstfile.raw firstfile.raw

The -aoptions immediately applies the similar transformation to all similar files at a nesting level 1 (i.e. for all other subfolders). If you do not trust the system, you can use other options such as -eto explain the transformation or -tto test it on all files.

这些-a选项会立即将类似转换应用于嵌套级别 1(即所有其他子文件夹)的所有类似文件。如果您不信任该系统,您可以使用其他选项,例如-e解释转换或-t在所有文件上测试它。

DISCLAIMER: I am a co-author of this work for academic purposes, and working on a bash script renderer. But the system is already available for testing purposes.

免责声明:出于学术目的,我是这项工作的合著者,并致力于 bash 脚本渲染器。但该系统已经可用于测试目的。