如何发现“修剪"行为? [英] How can this `find -prune`'s behavior be understood?
问题描述
根据GNU find
的联机帮助页,默认情况下使用-print
操作,并且
According to the manpage of GNU find
, -print
action is used by default and
禁止默认
-delete
,-exec
,-execdir
,-ok
,-okdir
,-fls
,-fprint
,-fprintf
,-ls
,和 -printf
.
Actions which inhibit the default
-delete
,-exec
,-execdir
,-ok
,-okdir
,-fls
,-fprint
,-fprintf
,-ls
,-printf
.
所以-prune
动作仍应暗示-print
动作.
So -prune
action should still imply -print
action.
实际上是.
$ tree .
.
├── dir/
│ └── file2
└── file1
$ find . -name dir #0
./dir
$ find . -name dir -prune #1
./dir #printed as expected
$ find . -name dir -prune -or -name file1 #2
./file1
./dir #printed as expected
但是,有时-prune
禁止使用默认的-print
.
However, sometimes -prune
inhibits the default -print
.
$ find . -name dir -prune -or -name file1 -print #3 #last -print is only added to the above example
./file1
$ find . -name dir -prune -or -print #4
.
./file1
我如何理解这个矛盾?
我的理解:
#1
-
file1
不能满足-name dir
的要求,因此被跳过.
file1
doesn't satify-name dir
so skipped.
dir
满足-name dir
的要求,因此将其修剪并将dir
添加到TODO列表中.
dir
satisfies -name dir
so pruned and dir
is added to TODO list.
-print
还将应用于TODO列表中的dir
.
-print
is additionally applied to dir
in TODO list.
#2
-
file1
满足-name file1
的要求,因此已添加到TODO列表中.
file1
satisfies-name file1
so added to TODO list.
与#1
-2
-print
还将应用于TODO列表中的dir
和file1
.
-print
is additionally applied to dir
and file1
in TODO list.
#3
-
与
#2
-1
与#2
-2
-print
应用于TODO列表中的file1
.
-print
is applied to file1
in TODO list.
-print
应该应另外应用于dir
,因为-prune
不会禁止-print
. (但这是不正确的.为什么?)
-print
should additionally be applied to dir
because -prune
doesn't inhibit -print
. (But this is incorrect. WHY?)
#4
-
file1
已添加到TODO列表.
file1
is added to TODO list.
与#3
-2
与#3
-3
与#3
-4
(实际上,find
中没有TODO列表.请参见标准.)
(Actually there is no TODO list in find
. See this comment and the standard.)
补充:
正如 oguz ismail的答案(现已删除)中指出的那样,我的问题与-prune
无关.但是,问题尚未解决.
As pointed out in oguz ismail's answer (deleted now), my question is not related to -prune
. However, the question is not solved.
让我们考虑一下-name A -o -name B -print
.这分为两个表达式:-name A
或-name B -print
.
Let us think about -name A -o -name B -print
. This is broken into two expressions: -name A
or -name B -print
.
我的理解:第一个表达式-name A
没有动作.因此,应隐含-print
.换句话说,-name A -o -name B -print
应该解释为-name A -print -o -name B -print
.
My understanding: The first expression -name A
doesn't have an action. So -print
should be implied. In other words, -name A -o -name B -print
should be interpreted as -name A -print -o -name B -print
.
实际行为:-name A -o -name B -print
是一种复合表达.此复合表达式中有-print
.因此,不应暗示任何其他-print
.
Actual behavior: -name A -o -name B -print
is one compound expression. There is -print
in this compound expression. So no additional -print
should be implied.
含糊不清,但我相信我的解释会更自然,因为在这种情况下,每个文件仅满足-name A
或-name B -print
(两个表达式从不同时满足)
There is ambiguity but I believe my interpretation is more natural because, in this case, only -name A
or -name B -print
is satisfied by each file (both expressions are never satisfied at the same time)
推荐答案
如此评论comment 是我的问题,该问题在OP的 Supplement 部分中进行了概述,该问题来自于GNU find
的联机帮助页含糊不清,而POSIX有更好的解释.我发现这是真的.
As written in this comment and this comment, my question, which is summarized in the Supplement section in OP, has come from the ambiguity in manpage of GNU find
and POSIX has a better explanation. I found this is true.
POSIX 说
(如果不存在表达式,则将
-exec
,-ok
或
(If no expression is present,
-exec
,-ok
, or
( given_expression ) -print
并且很自然地解释given_expression
是由一个或多个子表达式组成的复合表达式,因为它在括号中是封闭的. (如果此given_expression
引用单个子表达式,则括号肯定是多余的.)
and it is natural to interpret given_expression
is a compound expression which consists of one or more sub-expressions because it is closed in parenthesis. (If this given_expression
referred to a single sub-expression, the parenthesis would definitely be redundant.)
这篇关于如何发现“修剪"行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!