Reputation: 4815
According to the manpage of GNU find
, -print
action is used by default and
Actions which inhibit the default
-delete
,-exec
,-execdir
,-ok
,-okdir
,-fls
,-fprint
,-fprintf
,-ls
,-printf
.
So -prune
action should still imply -print
action.
Actually, it does.
$ 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
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
How can I understand this contradiction?
My Understanding:
#1
file1
doesn't satify -name dir
so skipped.
dir
satisfies -name dir
so pruned and dir
is added to TODO list.
-print
is additionally applied to dir
in TODO list.
#2
file1
satisfies -name file1
so added to TODO list.
same as #1
-2
-print
is additionally applied to dir
and file1
in TODO list.
#3
same as #2
-1
same as #2
-2
-print
is applied to file1
in TODO list.
-print
should additionally be applied to dir
because -prune
doesn't inhibit -print
. (But this is incorrect. WHY?)
#4
file1
is added to TODO list.
same as #3
-2
same as #3
-3
same as #3
-4
(Actually there is no TODO list in find
. See this comment and the standard.)
Supplement:
As pointed out in oguz ismail's answer (deleted now), my question is not related to -prune
. However, the question is not solved.
Let us think about -name A -o -name B -print
. This is broken into two expressions: -name A
or -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
.
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.
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)
Upvotes: 4
Views: 209
Reputation: 4815
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 says
(If no expression is present,
-exec
,-ok
, or
( given_expression ) -print
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.)
Upvotes: 2