bash 从所有子目录复制具有特定扩展名的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15617016/
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-09 23:29:20  来源:igfitidea点击:

Copy all files with a certain extension from all subdirectories

bashunixcp

提问by Abdel

Under unix, I want to copy all files with a certain extension (all excel files) from all subdirectories to another directory. I have the following command:

在unix下,我想将所有具有特定扩展名的文件(所有excel文件)从所有子目录复制到另一个目录。我有以下命令:

cp --parents `find -name \*.xls*` /target_directory/

The problems with this command are:

这个命令的问题是:

  • It copies the directory structure as well, and I only want the files (so all files should end up in /target_directory/)

  • It does not copy files with spaces in the filenames (which are quite a few)

  • 它也复制目录结构,我只想要文件(所以所有文件都应该在 /target_directory/ 中)

  • 它不会复制文件名中带有空格的文件(相当多)

Any solutions for these problems?

这些问题有什么解决方案吗?

回答by Brian Agnew

--parentsis copying the directory structure, so you should get rid of that.

--parents正在复制目录结构,所以你应该摆脱它。

The way you've written this, the findexecutes, and the output is put onto the command line such that cpcan't distinguish between the spaces separating the filenames, and the spaces withinthe filename. It's better to do something like

你写的这个问题的方法,在find执行时,其输出被放到命令行,从而cp不能空格分开的文件名和空间区分的文件名。最好做类似的事情

$ find . -name \*.xls -exec cp {} newDir \;

in which cpis executed for each filename that findfinds, and passed the filename correctly. Here's more infoon this technique.

其中cpfind找到的每个文件名执行,并正确传递文件名。这是有关此技术的更多信息

Instead of all the above, you could use zshand simply type

除了上述所有内容,您还可以使用zsh并简单地键入

$ cp **/*.xls target_directory

zshcan expand wildcards to include subdirectories and makes this sort of thing very easy.

zsh可以扩展通配符以包含子目录并使这种事情变得非常容易。

回答by guya

From all of the above, I came up with this version. This version also works for me in the mac recovery terminal.

综上所述,我想出了这个版本。这个版本也适用于我的 mac 恢复终端。

find ./ -name '*.xsl' -exec cp -prv '{}' '/path/to/targetDir/' ';'

It will look in the current directory and recursively in all of the sub directories for files with the xsl extension. It will copy them all to the target directory.

它将在当前目录中并递归地在所有子目录中查找具有 xsl 扩展名的文件。它会将它们全部复制到目标目录。

cp flags are:

cp 标志是:

  • p - preserve attributes of the file
  • r - recursive
  • v - verbose (shows you whats being copied)
  • p - 保留文件的属性
  • r - 递归
  • v - 详细(显示正在复制的内容)

回答by stingMantis

I had a similar problem. I solved it using:

我有一个类似的问题。我使用以下方法解决了它:

find dir_name '*.mp3' -exec cp -vuni '{}' "../dest_dir" ";"

The '{}'and ";"executes the copy on each file.

'{}'";"执行上的每个文件的副本。

回答by That Guy

I also had to do this myself. I did it via the --parents argument for cp:

我也必须自己做这件事。我是通过 cp 的 --parents 参数做到的:

find SOURCEPATH -name filename*.txt -exec cp --parents {} DESTPATH \;

回答by Camion

find [SOURCEPATH] -type f -name '[PATTERN]' | 
    while read P; do cp --parents "$P" [DEST]; done

you may remove the --parents but there is a risk of collision if multiple files bear the same name.

您可以删除 --parents 但如果多个文件具有相同的名称,则存在冲突的风险。