This guide is for Linux admins who use the find command on servers and want it to do exactly what they expect. The catch is that find does not read like most commands. After the starting paths, everything you type is a boolean expression, meaning a chain of true-or-false checks that find reads left to right for each file. Order matters, so -name x -delete removes only matching files, while -delete -name x empties the whole directory. The answer is one habit. Put the tests first, which are checks on a file’s name, type, size, age or permissions (-name, -type, -size, -mtime, -perm), and put the action last. Then run the command with -print before you switch it to -delete. The steps below apply this to four real server jobs, from finding what filled a disk to cleaning up stale files safely.
What you’ll accomplish
- Read any
findcommand as an expression and predict what it does before running it. - Track down what filled a disk, and which files changed since an incident began.
- Audit a tree for world-writable files, skipping the directories you do not care about.
- Delete stale files safely, using the
-print-first habit that makes-deleteboring. - Know when
-exec {} +matters and whenfindis the wrong tool entirely.
Prerequisites
This guide targets GNU findutils on Linux — what you get on Debian, Ubuntu, RHEL, Alpine and friends. Check with find --version. The differences from BSD/macOS find are real and will bite you:
-deleteexists on both, but GNU’s version implies-depth, which changes how it interacts with-prune(see Troubleshooting).-regexdiffers. GNU uses Emacs regex by default; BSD/macOS needsfind -Efor extended regex. The-regextypeoption is GNU-only.-printfis GNU-only. BSDfindhas no equivalent — use-exec statthere instead.-mtimerounding is the same on both, and it is the source of the off-by-one everyone hits.
You also want a shell that quotes properly (any POSIX shell) and, for the permission audit, an account that can read the tree — run those under sudo or expect gaps.
How it works
find takes a list of starting paths, walks the tree beneath each one, and for every file it encounters it evaluates your expression. The expression is built from tests (-name, -type, -mtime — these return true or false) and actions (-print, -delete, -exec — these do something and also return a value).
Two rules explain almost every surprise:
Adjacent terms are joined by an implicit -a (AND), and evaluation short-circuits. When find hits a false test, it stops evaluating that file and moves on. Everything to the right of the failed test never runs — including your action. That is why a well-ordered expression is also a safe one.
Implicit -a binds tighter than -o. So -name '*.gz' -o -name '*.log' -type f parses as -name '*.gz' -o ( -name '*.log' -a -type f ). The -type f applies only to the second branch — a directory called archive.gz still matches. If you meant “either name, but only files”, you must group explicitly with escaped parentheses: \( -name '*.gz' -o -name '*.log' \) -type f.
The lower half of that diagram is the whole safety story. In find /var/log -type f -mtime +30 -name '*.gz' -delete, the action only ever sees files that already passed three tests. Move -delete to the front and it is evaluated first: it returns true for every file it reaches, so the file is gone before -type is consulted. The tests still run — against a file that no longer exists.
Step-by-step
1. Make -print the default ending. Before any expression that acts, run the identical expression with -print. This is not advice you outgrow; it is how experienced people use find. -print is the implied action when you give none, so the shortest safe form is just the tests.
find /srv/app/tmp -type f -mtime +30 -name '*.tmp'
/srv/app/tmp/upload-8821.tmp
/srv/app/tmp/render-4410.tmp
/srv/app/tmp/session-902.tmp
2. Find what filled the disk. The classic 2 a.m. task. -size +100M finds large files; -xdev keeps find on one filesystem so it does not wander into /proc, /sys or a mounted backup share. GNU’s -printf prints size and path so you can sort numerically.
find /var -xdev -type f -size +100M -printf '%s\t%p\n' | sort -rn | head -5
3221225472 /var/lib/mysql/ibdata1
1073741824 /var/log/journal/9f2c41/system.journal
734003200 /var/log/nginx/access.log.1
262144000 /var/lib/docker/overlay2/8a3f/diff/tmp/core.2291
104857600 /var/backups/dump-2026-07-02.sql
Note the -size units: bare numbers mean 512-byte blocks, not bytes. Use c for bytes, k, M, G for the rest. Sizes are rounded up to the unit, which makes -size -1M nearly useless — a 100-byte file rounds up to 1M and fails the test, so only empty files match. Use -size -1024c when you mean small.
3. Find files changed since an incident started. Timestamps are the fastest way to answer “what changed?”. -mmin -30 means modified less than 30 minutes ago. For an arbitrary cutoff, create a reference file and use -newer, which compares against that file’s mtime — far easier to reason about than counting days.
touch -d '2026-07-16 02:15' /tmp/incident-start
find /etc /srv -xdev -type f -newer /tmp/incident-start
/etc/nginx/sites-enabled/api.conf
/srv/app/releases/8821/config.yml
Two files changed since 02:15. That is your blast radius. Prefer -newer or -mmin over -mtime whenever the window is smaller than a day — -mtime cannot express it.
4. Audit world-writable files. -perm has three modes and only one of them is usually right. -perm 644 matches exactly those bits. -perm -644 matches files with at least those bits (all of them set). -perm /644 matches any of them. For a world-writable audit you want “the other-write bit is set”, which is -perm -o+w (or -perm -0002 in octal — identical result).
find /srv/www -xdev -type f -perm -o+w
/srv/www/uploads/avatar-1204.png
/srv/www/config/legacy.ini
A world-writable config/legacy.ini is a genuine finding — any local user can rewrite it. Fixing this properly on a web tree is its own topic; see WordPress file permissions hardening for the ownership-and-mode model that keeps it fixed.
5. Skip directories with -prune. Audits get noisy when they descend into caches and backups. -prune tells find “do not descend into this” — it returns true and stops the walk there. It is almost always paired with -o, because the idiom is “either prune this, or do the real work”.
find /srv -xdev \( -path /srv/backups -o -path '*/node_modules' \) -prune -o -type f -perm -o+w -print
/srv/www/uploads/avatar-1204.png
/srv/www/config/legacy.ini
The trailing -print is mandatory here. Without it, find supplies a default -print that applies to the whole expression, including the pruned branch, and your excluded directories reappear in the output.
6. Delete stale files — -print first, then swap. Now combine it. Count, eyeball, then act. The expression never changes between the three commands; only the final action does.
find /srv/app/tmp -type f -mtime +30 -name '*.tmp' -print | wc -l
147
find /srv/app/tmp -type f -mtime +30 -name '*.tmp' -delete
-delete prints nothing on success. Do not read that silence as a safety net, and do not lean on “it refuses to remove non-empty directories” either: -delete implies -depth, so it empties a directory from the bottom up and then removes the directory that is, by then, empty. The only protection you actually rely on is that the tests come first.
7. Run a command on the matches. When you need more than deletion, -exec runs a command per match. The \; form runs it once per file; the + form appends as many files as fit on one command line and runs the command once. On large sets that is the difference between thousands of forks and a handful.
find /var/log -type f -name '*.log' -mtime +7 -exec gzip {} +
Use \; only when the command genuinely takes one file at a time. The xargs equivalent needs -print0 and xargs -0, which separate filenames with a null byte instead of whitespace — the only form that survives filenames containing spaces or newlines.
find /srv/www/uploads -type f -name '*.tmp' -print0 | xargs -0 rm -f
Validate it works
The check that matters is that your expression selects exactly the set you intend, before an action touches it. Count first:
find /srv/app/tmp -type f -mtime +30 -name '*.tmp' -print | wc -l
147
Then confirm the count went to zero afterwards, and that nothing else moved:
find /srv/app/tmp -type f -mtime +30 -name '*.tmp' -print | wc -l
find /srv/app/tmp -type f | wc -l
0
1289
147 stale files gone, 1289 current files untouched. If the second number dropped by more than 147, your expression was broader than you thought — which is exactly what the -print run was meant to catch.
Troubleshooting
“paths must precede expression”. You put a path after a test, or the shell ate an unquoted glob. This is the single most common find error:
find . -name *.log
find: paths must precede expression: 'b.log'
The shell expanded *.log against the current directory before find ran, so find received -name a.log b.log c.log and treated the extras as paths. Always single-quote patterns: find . -name '*.log'. find never sees the glob, and does the matching itself at every level of the tree.
-mtime +1 does not match yesterday’s file. -mtime counts whole 24-hour periods and truncates the remainder. A file modified 15 hours ago is “0 days” old, not 1. So +1 means “at least 2 full days ago”, and it matches neither yesterday afternoon’s file nor even -mtime +0. If you mean “in the last day”, use -mtime 0 or, better, -mmin -1440. For sub-day windows use -mmin or -newer — -mtime simply has no resolution there.
“Argument list too long”. This comes from find ... | xargs rm without null separation, or from a shell glob like rm /tmp/*.log, when the expanded list exceeds the kernel’s argument limit. -exec cmd {} + and -print0 | xargs -0 both fix it, because both split the work into batches that fit. This is the practical reason to prefer + over \; even when performance is not the concern.
Permission-denied noise drowns the output. Running find / as a normal user floods stderr with unreadable directories. 2>/dev/null hides the noise but also hides real errors, and you may be searching filesystems you do not mean to. Prefer scoping the search: -xdev stops find crossing into /proc, /sys, /run and network mounts, which removes most of the noise at the source rather than papering over it.
-prune and -delete do not work together. -delete implies -depth, which processes directory contents before the directory itself — and -prune has no effect during a depth-first walk. GNU’s manual says so plainly. If you need to exclude directories from a deletion, filter with -path ... -not style tests instead of -prune, and verify with -print before switching the action.
-maxdepth after a test triggers a warning. -maxdepth and -mindepth are global options, not tests — they apply to the whole command regardless of where you write them, so putting them late is misleading:
find: warning: you have specified the global option -maxdepth after the argument -type, but global options are not positional, i.e., -maxdepth affects tests specified before it as well as those specified after it. Please specify global options before other arguments.
Write them immediately after the paths: find /var/log -maxdepth 1 -type f.
When this is the wrong approach
find walks the tree and calls stat() on everything it touches. That is honest work, and sometimes it is the wrong work:
- Interactive searching. If you are hunting for a file by name and want an answer now,
fdis faster to type and faster to run; for content,ripgrepbeatsfind -exec grepby a wide margin.findearns its place in scripts and precise one-off filters, not as your daily search box. - Indexed lookups.
locate/mlocateanswers “where is this file” from a prebuilt database in milliseconds, without touching the disk. The trade-off is staleness — the index is only as fresh as the lastupdatedbrun. For “where did the package put its config”, that is usually fine. - The real question is disk usage. If you want to know what is consuming space rather than which individual files are large,
du -sh *orncduanswers directly and aggregates by directory.find -sizefinds big files; it will not tell you that four million small files filled the disk — or that you have run out of inodes rather than bytes. - The host is busy. A full-tree
findon a large filesystem is a sustained metadata read that competes with your database for IOPS and evicts useful pages from cache. On a loaded production box, scope it with-xdevand a narrow starting path, run it off-peak, or wrap it withionice -c3 nice. If a runaway process is the underlying problem, cgroup v2 resource control is the durable fix.
The authoritative reference is the find(1) man page, and the GNU findutils manual covers the expression grammar in more depth than any tutorial.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.