The shape of a find command
A find command has three parts: one or more starting points, then an expression, then nothing else. The starting points come first, and find walks each one in turn. The expression is read from left to right, and it is evaluated for each entry. If you omit the expression, find prints every name, which is the default action. Most of the power comes from combining tests with the operators and, or, and not.
cd /workspace
mkdir -p tree/src/lib tree/docs
touch tree/src/main.c tree/src/lib/util.c tree/docs/readme.md
find tree -name '*.c'The test -name matches the base name of each entry against a glob, and the quotes stop the shell from expanding the pattern before find sees it. The output lists the two C files, in the order the directory walk reaches them.
Common tests
The test -type selects an entry by its type, with f for a regular file, d for a directory, and l for a symbolic link. The test -size compares the size, and a suffix such as k or M gives the unit. The number is rounded up, so -size +1k selects entries of more than one kilobyte. The time tests -mtime, -atime, and -ctime count days, and -newer compares against another file. The test -empty selects empty files and empty directories.
The option -maxdepth limits how deep the walk goes, and -mindepth skips the shallower levels. These options are global, so they should appear before the tests. Use -maxdepth 1 when you want the entries of a directory without the recursion.
Actions and the default
The action -print writes each matching name followed by a newline. The action -print0 writes a NUL byte instead, which is safe for names that contain newlines. The action -delete removes each match, and -exec runs a command on each match. Adding an action changes the default: once you use -exec or -delete, the implicit print is off, so an expression with only a test prints the name and an expression with an action does what the action says.
cd /workspace
find tree -type f -name '*.md'
find tree -type d -maxdepth 1The first command prints the Markdown file. The second lists the directories directly under tree, including tree itself as the starting point.
Operators and grouping
Tests combine with -a for and, -o for or, and ! for not. The and operator is implied when you write two tests side by side. Parentheses group parts of the expression, and they must be escaped from the shell with a backslash or quotes, as in \( -name a -o -name b \). Precedence matters: and binds more tightly than or. When a search returns surprising results, add parentheses to make your intent explicit.
Read a complicated find expression as a list of questions asked of each entry. Write the pieces separately, test them, and only then join them with operators.
Pruning a directory
The action -prune tells find not to descend into the directory that matched. It is the standard way to skip a large directory, such as a version control folder. The pattern is find . -name .git -prune -o -type f -print, which means: if the entry is .git, prune it and print nothing; otherwise, print regular files. The or operator is what makes the pruned branch produce no output.
Time and size in practice
Time tests are days, not seconds, and the rounding can surprise you. The test -mtime 0 means within the last twenty-four hours, and -mtime +7 means older than a week. Size tests are exact when you use bytes with c, so -size 0c is clearly empty. Use -newer with a reference file to compare modification times exactly, which is the most reliable option in scripts.
cd /workspace
head -c 5000 /dev/zero > tree/big.bin
find tree -type f -size +1kThe output is the big file, because its size is more than one kilobyte. The empty and small files are not listed.
Reading find output safely
The default output of find is one name per line, which is fine when you read it yourself and dangerous when a program parses it. A name that contains a newline appears as two lines, and a name that begins with a dash may look like an option to the next command. Use -print0 when the output is consumed by a program, and use the -- marker or an explicit path such as ./name when you pass the names to a command.
Relative starting points are printed relative to what you typed. If you run find tree, the output begins with tree, and if you run find ./tree, the output begins with ./tree. Both name the same files, but a script that compares the output with a stored list must use the same form each time.
Searching by time and owner
The test -newer compares the modification time of each entry with a reference file, and it is precise because it uses full timestamps rather than whole days. A common backup check is find with -newer against a stamp file created at the end of the last backup, which lists exactly the files that changed since then. Create the stamp file after the backup finishes, not before, so that a file changed during the backup is not missed.
The test -user selects entries owned by a given user name, and -group selects by group. These tests are useful when cleaning up after a program that ran as a different account. Read the permissions chapter before you change ownership, because find shows you what owns a file but does not decide whether you are allowed to change it.
find and the order of results
find visits directory entries in the order the filesystem returns them, which is not sorted and can change after the directory is modified. A script that needs a stable order must sort the output, and the sort must be on the same field each time. Sorting also makes diffs between two runs meaningful, which is useful for checking that a cleanup did what it should.
Use find with -maxdepth to keep a walk bounded, and combine it with -xdev when you want to stay on one filesystem. A walk that crosses into a network mount or a large backup disk can take far longer than you expect, so decide the boundary before you start.
Practice in the lab
The lab builds a small tree and asks you to find C files, list the directories, and select the large file by size. Each result is saved, and the checker compares the sorted output so that the directory order does not matter.