Is -print a useless option for find now?
Solution 1:
Looking at the man page under FreeBSD, I see:
-print This primary always evaluates to true. It prints the pathname of
the current file to standard output. If none of -exec, -ls,
-print0, or -ok is specified, the given expression shall be
effectively replaced by ( given expression ) -print.
So in many cases, -print
is unnecessary. However, consider this expression that looks for a file named foo
inside of somedir
, but not inside any directory named .snapshot
:
find somedir -name .snapshot -prune -o -name foo
Given the description referenced above, this will be transformed into:
find somedir ( -name .snapshot -prune -o -name foo ) -print
Which is not the same as what was probably intended:
find somedir -name .snapshot -prune -o -name foo -print
Adding parentheses to make the group a bit more obvious, this is:
find somedir ( -name .snapshot -prune ) -o ( -name foo -print )
To spot the difference, notice that both -prune
and -print
evaluate to true
. So without specifying -print
, the first version will print out the current file if either -name .snapshot
or -name foo
matches.
The second version will only output the current file if -name foo
matches.
This is a long winded way of saying that -print
is not generally necessary as long as you understand the situations in which it is necessary.