问题描述
这部分(-name * txt -o -name * html)使我在代码中感到困惑:
find $ HOME \(-name \ * txt -o -name \ * html \)-print0 | xargs -0 grep -li vpn
有人可以解释括号和-o吗? -o是命令还是参数?我知道括号是被\转义的,但是为什么呢?
默认情况下,找到参数列表是'和'在一起。 -o
选项表示'or'。
如果您写道:
find $ HOME -name \ * txt -o -name \ * html -print0
那么没有与文件名相关的输出操作以'txt'结尾,所以它们不会被打印。通过将名称选项与括号分组,您可以同时获得'html'和'txt'文件。
:
mkdir test-find
cd test-find
cp / dev / null file.txt
cp / dev / null file.html
下面的评论有一个有趣的方面这个。如果该命令是:
find。 -name'* .txt'-o -name'* .html'
然后,行动被指定为替代方案,则对于这两个替代方案都使用默认的 -print
(而不是 -print0
!两个文件都列出来了。使用 -print
或其他显式操作(其他操作之一)后,只有替换操作才能生效。
find。 -name'* .txt'-print -o -name'* .html'
这也暗示了你可以有不同的选择不同的行动。
您也可以应用其他条件,例如修改时间:
$ $ p $ find。 \(-name'* .txt'-o -name'* .html'\)-mtime +5 -print0
find。 \(-name'* .txt'-mtime +5 -o -name'* .html'\)-print0
第一次打印超过5天的txt或html文件(因此它不会为示例目录打印任何内容 - 文件只有几秒钟的时间);第二个打印超过5天的txt文件或任何年龄的HTML文件(所以只是file.html)。等等...
感谢DevSolar对他的评论,导致了这一点。
This part " ( -name *txt -o -name *html ) " confuses me in the code:
find $HOME \( -name \*txt -o -name \*html \) -print0 | xargs -0 grep -li vpn
Can someone explain the the brackets and "-o"? Is "-o" a command or a parameter? I know the brackets are escaped by "\" , but why are they for?
By default, the conditions in the find argument list are 'and'ed together. The -o
option means 'or'.
If you wrote:
find $HOME -name \*txt -o -name \*html -print0
then there is no output action associated with the file names end with 'txt', so they would not be printed. By grouping the name options with parentheses, you get both the 'html' and 'txt' files.
Consider the example:
mkdir test-find
cd test-find
cp /dev/null file.txt
cp /dev/null file.html
The comments below have an interesting side-light on this. If the command was:
find . -name '*.txt' -o -name '*.html'
then, since no explicit action is specified for either alternative, the default -print
(not -print0
!) action is used for both alternatives and both files are listed. With a -print
or other explicit action after one of the alternatives (but not the other), then only the alternative with the action takes effect.
find . -name '*.txt' -print -o -name '*.html'
This also suggests that you could have different actions for the different alternatives.You could also apply other conditions, such as a modification time:
find . \( -name '*.txt' -o -name '*.html' \) -mtime +5 -print0
find . \( -name '*.txt' -mtime +5 -o -name '*.html' \) -print0
The first prints txt or html files older than 5 days (so it prints nothing for the example directory - the files are a few seconds old); the second prints txt files older than 5 days or html files of any age (so just file.html). And so on...
Thanks to DevSolar for his comments leading to this addition.
这篇关于在查找中分组谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!