27

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
41

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.

  • Sort of beginner level question but what's this expression you are talkng about? is this regular expression? – Rishi Prakash Apr 20 '17 at 14:33
  • 2
    @Rishi see the manpage. – Stephen Kitt Apr 20 '17 at 14:35
20

-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

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.

  • If it is non-standard and is best avoided why name it at all? – Isaac Nov 27 '18 at 22:19
  • @Isaac, because they can be handy/are possible typos... – vonbrand Nov 28 '18 at 0:31
8

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

4

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.

  • 3
    That would be very very old versions of AIX then. AIX 4.3 find doesn't need it. AIX has been POSIX conformant for decades. – Stéphane Chazelas Apr 22 '15 at 9:13
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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.