WSU logo


College of Engineering & CS
Wright State University
Dayton, Ohio 45435-0001

CEG 333: Introduction to Unix

Prabhaker Mateti

find


The find utility does exactly what its name suggests: find files that meet given criteria. It can also run a command on each of these files.

The basic syntax of is find PATH... EXPRESSION..., where an expression can be an option, a test, or an action to be taken. Advanced usage of find combines many expressions to perform complex tasks on a file hierarchy, but these are the basics:

Test Finds Files...
-name PATTERN with basename matching PATTERN in FNRE syntax.
-path PATTERN with full paths matching PATTERN in FNRE syntax.
-regex REGEX with full paths matching REGEX in SMRE syntax. Use single quotes.
-type TYPE which are of TYPE. Types include "f" for files, "d" for directories, and "l" for links.
-lname PATTERN which are symbolic links pointing at a file matching PATTERN in FNRE syntax.
-user NAME owned by the user NAME.
-group NAME owned by the group NAME.

Some tests have also have a case-insensitive version (such as -iname, -iregex, -ipath, and -ilname).

Note: most tests expect FNRE shell patterns as used on the Bash command line. If these tests are given an expression in SMRE syntax, they will not find the expected files. This is the difference between "-path" and "-regex" above.

Action Effect
-exec COMMAND ; Run COMMAND. Any tokens between "-exec" and ";" are its arguments. Wherever the string "{}" is present, the current filename will be substituted.
-print Print the current filename. This is the default action if none is specified.
-fprint FILE Write the current filename to FILE.

Warning: remember to properly quote and/or escape everything to avoid unexpected results from the shell.

Example: find . /tmp -user "$USER" -name '*tmp[0-9]*' -print -exec rm \{\} \;