find – find files


Environment Variables
See Also


find pathname-list expression


Find recursively descends the directory hierarchy for each pathname in the pathname-list (i.e., one or more pathnames) seeking files that match a boolean expression written in the primaries given below. In the descriptions, the argument n is used as a decimal integer where +n means more than n, -n means less than n and n means exactly n.

-name filename

The primaries may be combined using the following operators (in order of decreasing precedence):

1) A parenthesized group of primaries and operators (parentheses are special to the Shell and must be escaped).

Options have been introduced by POSIX.1-2001 in addition to the expression operators. They must preceed the pathname-list one the command line and have no effect on boolean expression processing.

-H Follow symbolic links given on the command line, but do not follow symbolic links encountered during directory traversal.

With the -follow primary or the -L option, hierarchy loops caused by symbolic links are detected, but only /usr/5bin/posix2001/find prints an error message. The offending link is not followed, and operation continues with the next directory entry found.


To remove all files named ‘a.out’ or ‘*.o’ that have not been accessed for a week:

find / \( -name a.out -o -name ‘*.o’ \) -atime +7 -exec rm {} \;

The rm command is executed once for each file. The form

find / \( -name a.out -o -name ‘*.o’ \) -atime +7 -exec rm {} +

is faster since the rm command is executed with a set of pathnames.

To find all files below the directory ‘documents’ that contain the regular expression ‘string’:

find documents -type f -exec grep string {} +

To find all files in the directory ‘home’, not descending into its subdirectories:

find home ! -name home -prune

To check whether the file ‘diary’ has been updated within the last two days; the name of the file is printed if true, and is not printed otherwise:

find diary -prune -mtime -2






chmod(1), cpio(1), pax(1), sh(1), xargs(1), stat(2), glob(7), locale(7)


Undesired effects can result if file names printed by find contain newline characters, as shown by the following command sequence:

$ mkdir -p 'dummy
> /etc'
$ touch 'dummy
> /etc/passwd'
$ find . -print


Shell scripts or utilities unaware of this problem will operate on /etc/passwd (or other arbitrary file names) when reading such output; a malicious user might create such files to read or overwrite privileged information. To circumvent this problem, one of the following proposals should be taken unless the file hierarchy traversed by the find command is definitively known not to contain such file names:

- If the output is read by the xargs utility to gain faster execution by aggregating command arguments as in find . -print | xargs command a safe and equally fast substitute is the find . -exec command {} +

operand of find; it is not portably accepted by find implementations, though.

The -print0 operand supported by some other implementations is considered a very limited work-around since it does not allow the output to be processed by utilities unaware of NUL characters; it has therefore not been included here.