find: The Linux Command Whose Man Page Nobody Finished

The find command is older than most of us and smarter than we give it credit for. Here's how its tests, its time maths and its actions really work, plus the -delete mistake that empties a folder.

Tux Harlow

3 min read

The Linux find command is one of the oldest tools on the system and one of the least understood. Most people know find . -name and stop there. This post covers how find actually reads its arguments, the time and size tests, the actions, and the one mistake with -delete that you only make once.

How find reads a command line

A find command is a starting point followed by an expression. The expression is made of tests (does this file match?), actions (do something with it) and operators (and, or, not). Everything is evaluated left to right, file by file, and evaluation stops for a file as soon as the result is known.

Bash
find /var/log -type f -name '*.log'

This walks /var/log and prints regular files whose names end in .log. Two tests joined by an implicit "and". When there is no action, find adds -print for you.

Quote your patterns. Without the quotes the shell may expand *.log against the current directory before find ever sees it.

Names, types and depth

Bash
find . -iname 'readme*' -type f
find /etc -maxdepth 1 -type l
find . -type d -empty

The first is case-insensitive. The second lists symbolic links directly in /etc without descending. The third lists empty directories. Note that -maxdepth is an option, not a test, and GNU find warns if you put it after a test.

Time is measured in whole days, rounded down

Bash
find /tmp -type f -mtime +7
find . -mmin -30

-mtime counts in 24-hour periods and discards the fraction. So -mtime +1 matches files modified at least two days ago, not one. The man page says so; most people never read that far. -mmin works in minutes and is usually what you meant.

Size has its own rounding surprise

Bash
find / -xdev -type f -size +100M

This finds files over 100 MiB on the root filesystem only (-xdev stops it crossing into other mounts). The surprise: sizes are rounded up to the unit. -size -1M does not mean "under a megabyte". It matches only empty files, because anything with one byte rounds up to 1M. Use -size -1024k or -size -1048576c when you need the real comparison.

Doing things: -exec, + and -print0

Bash
find . -name '*.bak' -exec rm -- {} +
find . -type f -print0 | xargs -0 grep -l 'TODO'

-exec with \; runs the command once per file. With + it packs many file names into each run, which is much faster. -print0 and xargs -0 separate names with a null byte, so file names with spaces or newlines survive the trip.

-delete, and the order that matters

Bash
find . -name '*.tmp' -delete

-delete is an action, and find evaluates left to right. If you write find . -delete -name '*.tmp', -delete runs first, on every file, and the name test never matters. Always put -delete last, and run the same command with -print first to see what it will remove. -delete also turns on -depth, which matters if you combine it with -prune.

Pruning directories you never want

Bash
find . -path ./node_modules -prune -o -name '*.js' -print

This skips node_modules entirely and prints .js files everywhere else. The explicit -print is needed: without it, find would print the pruned directory too.

Try it

Run find . -newer somefile and find . -perm -u+x -type f on a project you know, and read the EXPRESSION section of man find once. It's shorter than you think.