查找-名称“*.xyz” -o -名称“*.abc” -exec 对所有找到的文件执行,而不仅仅是指定的最后一个后缀
我正在尝试运行
find ./ -name "*.xyz" -o -name "*.abc" -exec cp {} /path/i/want/to/copy/to
事实上,它是一个更大的扩展名列表,但我不知道这对于这个例子来说很重要。基本上我想将所有找到的内容复制到另一个/path/i/want/to/copy/to。然而,它似乎只执行列表中的姓氏测试。
如果我删除 -exec 部分,我期望找到的所有文件变体都会打印出来。
如何让它将找到的完整文件传递给 -exec?
I'm trying to run
find ./ -name "*.xyz" -o -name "*.abc" -exec cp {} /path/i/want/to/copy/to
In reality it's a larger list of name extensions but I don't know that matters for this example. Basically I'd like to copy all those found to another /path/i/want/to/copy/to. However it seems to only be executing the last -name test in the list.
If I remove the -exec portion all the variations of files I expect to be found are printed out.
How do I get it to pass the full complement of files found to -exec?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
find
的工作原理是评估您提供的表达式,直到它可以确定整个表达式的真值(true 或 false)。在您的情况下,您实际上是在执行以下操作,因为默认情况下它将表达式与在一起。引用手册页:
这意味着如果名称与
*.xyz
匹配,它甚至不会尝试检查后面的-name
测试或-exec
,因为这已经是事实了。您想要做的是强制优先级,您可以使用括号来做到这一点。烦人的是,您还需要使用反斜杠在 shell 上转义它们:
find
works by evaluating the expressions you give it until it can determine the truth value (true or false) of the entire expression. In your case, you're essentially doing the following, since by default it ANDs the expressions together.Quoth the man page:
That means that if the name matches
*.xyz
, it won't even try to check the latter-name
test or-exec
, since it's already true.What you want to do is enforce precedence, which you can do with parentheses. Annoyingly, you also need to use backslashes to escape them on the shell:
比 Jaypal 的解决方案更有用的可能是:
More usable than Jaypal's solution would maybe be:
它可能有效:
It may work: