$_ CLI In Depth
Learning progress 0%
lesson files globbing find

Finding Files with find: Tests & Actions

6 min read Module 4: File Manipulation, Globbing & find Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:19

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.

BASH4 lines
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.

BASH3 lines
cd /workspace
find tree -type f -name '*.md'
find tree -type d -maxdepth 1

The 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.

Tip

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.

BASH3 lines
cd /workspace
head -c 5000 /dev/zero > tree/big.bin
find tree -type f -size +1k

The 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.

find: walk, test left to right, then act

Visualizer

find walks a tree and checks each entry against a list of tests, left to right, stopping at the first false one. The action decides what happens to entries that pass.

1 / 6
  • .
  • src
  • main.c
  • util.h
  • tests
  • t1.c
test, left to rightresult
-name '*.c'no
-type fnot checked
-printnothing printed
$ find . -name '*.c' -type f

find starts at . and tests that entry first. The name test fails, so -type f is skipped. -and is implied between tests, and evaluation stops at the first false one.

cd "$(mktemp -d /tmp/find.XXXX)" && mkdir -p src tests && touch src/main.c src/util.h tests/t1.c && find . -name "*.c" -type f
Terminal lab · 3 steps · real Linux in your browser

Find files, directories and large files

Select C files by name, list directories, and find files above one kilobyte.

01
Find files by name
Save the sorted list of C files under tree: ``bash find tree -name '*.c' | sort > c-files.txt ``
Show hint for step 1
Quote the pattern so the shell passes it to find unchanged.
02
Find directories
Save the sorted list of directories under tree, including tree itself: ``bash find tree -type d | sort > dirs.txt ``
Show hint for step 2
-type d selects directories.
03
Find files larger than one kilobyte
Create a file of 5000 bytes at tree/big.bin, then save the regular files larger than one kilobyte: ``bash head -c 5000 /dev/zero > tree/big.bin find tree -type f -size +1k > large.txt ``
Show hint for step 3
-size +1k means larger than one kilobyte.
Check yourself Question 1 of 4
Concept Check Regular files recall

Which find test selects regular files?

Recap & Key Takeaways

find takes starting points and an expression, and it evaluates the expression for every entry it visits.
Tests such as -name, -type, -size and -mtime select entries, and -print0 makes output safe for names with newlines.
Adding an action such as -exec or -delete turns off the implicit print, so the expression must do what you intend.
Use -prune with -o to skip a directory, and parentheses to make the order of and and or explicit.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue