Unix/Linux 中特定文件的递归副本?

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

Recursive copy of specific files in Unix/Linux?

linuxbashshellunixcp

提问by Yury Pogrebnyak

I need to copy all *.jarfiles from directory and all its subdirectories. How can I do it in UNIX/Linux terminal? Command cp -r *.jar /destination_dirdoesn't work.

我需要*.jar从目录及其所有子目录中复制所有文件。如何在 UNIX/Linux 终端中执行此操作?命令cp -r *.jar /destination_dir不起作用。

采纳答案by Sean

rsyncis useful for local file copying as well as between machines. This will do what you want:

rsync对于本地文件复制以及机器之间的复制很有用。这将执行您想要的操作:

rsync -avm --include='*.jar' -f 'hide,! */' . /destination_dir

rsync -avm --include='*.jar' -f 'hide,! */' . /destination_dir

The entire directory structure from . is copied to /destination_dir, but only the .jar files are copied. The -a ensures all permissions and times on files are unchanged. The -m will omit empty directories. -v is for verbose output.

整个目录结构来自 . 复制到 /destination_dir,但仅复制 .jar 文件。-a 确保文件的所有权限和时间不变。-m 将省略空目录。-v 用于详细输出。

For a dry run add a -n, it will tell you what it would do but not actually copy anything.

对于试运行,添加一个 -n,它会告诉你它会做什么,但实际上不会复制任何东西。

回答by Pavan Manjunath

tar -cf - `find . -name "*.jar" -print` | ( cd /destination_dir && tar xBf - )

回答by glenn Hymanman

find . -name \*.jar | xargs cp -t /destination_dir

Assuming your jar filenames do not contain spaces, and your cphas the "-t" option. If cpcan't do "-t"

假设您的 jar 文件名不包含空格,并且您cp有“-t”选项。如果cp不能做“-t”

find . -name \*.jar | xargs -I FILE cp FILE /destination_dir

回答by Bartosz Moczulski

cp --parents `find -name \*.jar` destination/

from man cp:

来自man cp

--parents
       use full source file name under DIRECTORY

回答by user unknown

If your find has an -exec switch, and cp an -t option:

如果您的 find 有一个 -exec 开关,并且有一个 -t 选项:

find . -name "*.jar" -exec cp -t /destination_dir {} +

If you find doesn't provide the "+" for parallel invocation, you can use ";" but then you can omit the -t:

如果你发现没有为并行调用提供“+”,你可以使用“;” 但是你可以省略-t

find . -name "*.jar" -exec cp {} /destination_dir ";"

回答by Idelic

If you want to maintain the same directory hierarchy under the destination, you could use

如果要在目标下保持相同的目录层次结构,可以使用

(cd SOURCE && find . -type f -name \*.jar -exec tar cf - {} +) \
  | (cd DESTINATION && tar xf -)

This way of doing it, instead of expanding the output of findwithin back-ticks, has the advantage of being able to handle any number of files.

这种做法find的优点是能够处理任意数量的文件,而不是在反引号内扩展输出。

回答by uzsolt

If you don't need the directory structure only the jar files, you can use:

如果你不需要目录结构只需要 jar 文件,你可以使用:

shopt -s globstar
cp **/*.jar destination_dir

If you want the directory structure you can check cp's --parentsoption.

如果你想要目录结构,你可以检查cp's--parents选项。