我如何 运行 在 Linux 中同时查找和 cp 命令?

How do I run find and cp commands concurrently in Linux?

如何同时执行 运行 findcp 命令?我试过这个:

find -name "*pdf*" | xargs cp  ./

但是没用。

使用-exec选项:

find ./ -name "*pdf*" -exec cp -t . {} \+

{} 替换为当前正在处理的文件名。

来自 find 的手册页:

-exec command {} +

...the command line is built by appending each selected file name at the end.. The command line is built in much the same way that xargs builds its command lines.

注意使用 -t(目标目录)选项(这是一个 GNU 扩展)。我们不能使用 -exec cp {} . +,因为匹配的文件名会附加到命令行的末尾,而目标必须最后指定。另一种解决方法是调用 sh:

find ./ -name "*pdf*" -exec sh -c 'cp "$@" .' '' {} +

我习惯性地转义了+这个字符。请注意,您应该转义 find 语法的特殊字符,以防止它们被 shell 扩展。特别是,在 + 之前可能不需要反斜杠,因为大多数 shell 会将其解释为字符串(不会扩展为不同的内容)。但是,您肯定必须 escape/quote ;(按顺序将命令应用于每个文件):

find -name "*pdf*" -exec cp -f {} . ';'