The man page of Debian 8's find command says:
If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.
So why do these outputs differ:
$ mkdir -p test/foo test/bar && cd test && touch foo/bar bar/foo
$ # Test 1
$ find . -name foo -type d -prune -o -name foo
./foo
./bar/foo
$ # Test 2
$ find . -name foo -type d -prune -o -name foo -print
./bar/foo
So test 1: does the expression contain "no actions other than -prune or -print?" Well, excluding the prune, yes that statement is true, there are no actions. So these results are expected since for ./foo
the expression before the -o
option returns True, so it's printed.
But test 2: does the expression contain "no actions other than -prune or -print?" Well, excluding the prune and the print, yes that statement is true again, there are no other actions. So I would expect the same results.
But I don't get ./foo
. Why?
It's as if the man page should read: "If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true."
I'm going with the simpler explanation, the man page is wrong. It should instead say
It should also maybe contain a caveat for
-quit
, which is an action, but it causes-find
to exit immediately. So even though an implicit-print
is added for the whole expression it is never actually executed.The posix find man page contains a clearer explanation, though it doesn't have quite as many actions as the expanded
gnu
version.Out of what
gnu
calls actions, posix only defines-exec
,-ok
,-print
, and-prune
. It does not have any of the expanded actions-delete
,-ls
, etc... So the definition matches the correctedgnu
one by only omitting-prune
.Here are some examples using all the gnu
find
actions which prove the point. For all consider the following file structure-delete
-exec command ;
-execdir command {} +
-fls
-fprint
-ls
-ok command ;
-okdir command ;
-print
-print0
-printf
-prune
-quit
Check the GNU Findutils manual, it says
Apparently, debian's manual is wrong, because it's just a GNU Find. And I have no idea why this happened, since it's just a copy to me.
Let's look at this command:
Since
-print
is the default action, then this action is applied to the whole set of expressions, i.e.-name foo -type d -prune -o -name foo
. So it's the same as the following:Now let's look at this command:
According to
man find
expr1 expr2
has higher priority thanexpr1 -o expr2
. So in the command above two expressions are combined with the OR operator:-name foo -type d -prune
-name foo -print
So if you want to apply
-print
to both, use parentheses:But
-prune -o RHS
implies thatRHS
is evaluated only for those items which didn't get pruned.We can check if we are right by running
find
with-D tree
or-D opt
:As we can see,
find
makes(... -prune) -o (... -print)
from the first expression where we put-print
explicitly. It makes(...) -a -print
from the second expression where we omit-print
.So I think that by "the whole expression" the man page means one of expression parts described in
OPERATORS
section.