且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找到-name" * XYZ" -o -name" * ABC&QUOT。 -exec来执行所有找到的文件,指定不只是过去的后缀

更新时间:2023-02-17 13:21:16

找到的工作原理是评估前pressions你给它,直到它可以确定真值整个前pression的(true或false)。在你的情况,你基本上执行以下操作,因为默认情况下它ANDS除权pressions在一起。

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.

-name "*.xyz" OR ( -name "*.abc" AND -exec ... )

答曰手册页:

GNU find搜索
         通过评估在每个给定文件名根的目录树
         给定的前pression从左到右,根据precedence的规则(见节算),直到结果是已知的(左
         手侧为假并操作,或真),在该点
         找到下一个文件名的动作。

GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

这意味着,如果名称匹配 * XYZ ,它甚至不会尝试检查后 -name 测试或 -exec ,因为它已经成为事实。

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.

您想要做的是执行precedence,您可以用括号做。烦人的,你还需要使用反斜杠逃脱他们的外壳:

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:

find ./ \( -name "*.xyz" -o -name "*.abc" \) -exec cp {} /path/i/want/to/copy/to \;