简体   繁体   English

为什么我的 find 和 xargs 复制命令适用于一个文件夹而不适用于另一个文件夹?

[英]Why is my find and xargs copy command working for one folder and not for the other?

I have two directories, x86_64 and i386 .我有两个目录, x86_64i386 I want to filter out test RPMS from both of these folders and place them in a seperate one;我想从这两个文件夹中过滤掉测试 RPMS 并将它们放在一个单独的文件夹中; test_release/{version}-x86_64/x86_64 and test_release/{version}-i386/i386 , respectively. test_release/{version}-x86_64/x86_64test_release/{version}-i386/i386分别。

So my first command works fine:所以我的第一个命令工作正常:

find x86_64/ -type f -name '*test*' -o -name '*demo*' -o -name '*log*' |
xargs cp -rt test_release/${RELEASE}-x86_64/x86_64

My second command is exactly the same, except with different folder names:我的第二个命令完全相同,只是文件夹名称不同:

find i386/ -type f -name '*test*' -o -name '*demo*' -o -name '*log*' |
xargs cp -rt test_release/${RELEASE}-i386/i386

Only the second command gives me the error:只有第二个命令给了我错误:

cp: missing file operand

Am I missing something?我错过了什么吗?

The error happens because your find command doesn't return any files.发生错误是因为您的find命令不返回任何文件。 Disappointingly, xargs still runs cp , but ends up calling it with too few arguments;令人失望的是, xargs仍然运行cp ,但最终以太少的 arguments 调用它; and so you get the error message.所以你得到错误信息。

GNU xargs has an -r option which solves this specific case; GNU xargs有一个-r选项可以解决这种特殊情况; but a more portable and more robust solution is to use find -exec .但更便携和更健壮的解决方案是使用find -exec

find i386/ -type f -name '*test*' -o -name '*demo*' -o -name '*log*' \
    -exec cp -rt "test_release/${RELEASE}-i386/i386" {} +

The + statement terminator for -exec is not entirely portable, but if you have cp -t I guess you're on Linux, or at least are using a recent find (GNU or not GNU. If not, replacing + with \; is a workaround, though it will end up running more processes than the equivalent xargs construct). -exec+语句终止符不是完全可移植的,但如果你有cp -t我猜你在 Linux 上,或者至少正在使用最近的find (GNU 或不是 GNU。如果不是,用\;替换+是一种解决方法,尽管它最终会运行比等效的xargs构造更多的进程)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM