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.
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
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
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
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
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
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
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.
Linux
More like this
Linux
entr and watch: Make Your Terminal Rerun Things So You Don't Have To
Save, switch window, press up, press Enter, squint. Save, switch window, press up... Stop. entr reruns your command whenever a file changes, and watch reruns it on a timer. Two tiny tools, a thousand saved keystrokes.
Linux
ripgrep: grep, But It Drank Three Espressos and Read Your .gitignore
grep is a legend. It's also happy to search all 80,000 files in node_modules while you wait. ripgrep searches only what matters, very quickly, with sensible defaults. The flags worth knowing, and when old grep still wins.
Linux
Linux Terminal Toys (and the Surprisingly Useful Tools Hiding Among Them)
A talking cow, a train that punishes typos, the Matrix in your terminal, and hidden among them, six genuinely useful tools you'll keep long after the novelty wears off. A toy box for Linux nerds.