41

What is the difference between:

find .

and

find . -print

What does -print actually do?

$ find .
.
./hello.txt
./hello
./hello/txt
./hello/hello2
./hello/hello2/hello3
./hello/hello2/hello3/txt
./hello/hello2/txt
$ find . -print
.
./hello.txt
./hello
./hello/txt
./hello/hello2
./hello/hello2/hello3
./hello/hello2/hello3/txt
./hello/hello2/txt
faressoft
  • 735

6 Answers6

51

From the findutils find manpage:

If no expression is given, the expression -print is used (but you should probably consider using -print0 instead, anyway).

(-print is a find expression.)

The POSIX documentation confirms this:

If no expression is present, -print shall be used as the expression.

So find . is exactly equivalent to find . -print; the first has no expression so -print is added internally.

The explanation of what -print does comes further down in the manpage:

-print

True; print the full file name on the standard output, followed by a newline. If you are piping the output of find into another program and there is the faintest possibility that the files which you are searching for might contain a newline, then you should seriously consider using the -print0 option instead of -print. See the UNUSUAL FILENAMES section for information about how unusual characters in filenames are handled.

Stephen Kitt
  • 434,908
25

-print is the default action. Some find predicates are considered as actions as opposed to filters or conditions. For instance, -type f is not an action. -exec is an action even though it can also be used as a condition.

Actions include -print, -exec and -ok. Some find implementations have other non-standard action predicates like the -print0, -printf, -execdir, -okdir, -ls...

find files <some-predicates>

Where none of <some-predicates> contain actions is equivalent to:

find files \( <some-predicates> \) -print

(note the parentheses above which are important if there are some -o operators).

When in doubt, best is to use -print explicitly (or -exec printf '%s\0' {} + (or -print0 where available) so that output can be post-processed).

The default -print action is specified by POSIX. Some old find implementations required an explicit -print, but those are usually not found in the wild nowadays.

Also note that some find implementations allow omitting the files, in which case they default to searching into the current directory. That is, for them,

find
find -print

is equivalent to

find .
find . -print

That is however not standard, so is best avoided.

On the more verbose (and useful) end of the spectrum, some find implementations also allow passing file paths as argument to a -f option as in:

find -f "$file1" -f "$file2" -print

They are the only find implementations that allow passing arbitrary file paths to find. Other implementations cannot accept file paths like ! or -print... so find "$file" -print (or even find -- "$file" -print) assumes $file is not the name of a find predicate (or option in the first case).

Unfortunately that's not standard nor portable either.

9

They are the same, they both write out the entire directory hierarchy from the current directory.

From POSIX find documentation:

The following commands are equivalent:

find .

find . -print

cuonglm
  • 153,898
3

In Linux there is no difference, but other systems (like AIX for instance) need -print if you want the output of the command displayed on your screen.

YoMismo
  • 4,015
3

For many years the find command did not have a default action. A common error was forgetting to add the -print option to your find command. I still to this day type it out of habit.

But at some point it was added as the default action so now find . and find . -print are equivalent.

Kevin
  • 39
2

It is sometimes useful to use -print explicitly when you are performing another action so the filename is displayed as that action is performed.

find . -print -delete

would be similar to

rm -rfv *

where -print corresponds to -v. If you don't include -print then the filenames aren't displayed.

In order to make the rm command even more similar, by the way, issue this Bash command first

shopt -s dotglob

which will make the * match dot (hidden) files.

  • This does not answer the question. The question asks why find . and find . -print seemingly generates identical output and what the role of -print is in that example. Your answer seems to want to talk about -print in general terms, which would have been ok if you also had answered the question. – Kusalananda Jan 29 '23 at 10:27