How is a find command built?
find [starting points] [expression]. The expression is a chain of tests, such as -name or -mtime, and actions, such as -print or -delete. find evaluates the chain left to right for each file and stops at the first false test. Without an action, it prints matching paths.
find /var/log -type f -name '*.gz' -mtime +30
Read it as: under /var/log, regular files, whose names end in .gz, last modified more than 30 days ago.
Which tests are used most?
| Test | Matches |
|---|---|
-name '*.php', -iname | Base name, case-sensitive or not |
-path '*/cache/*' | Whole path pattern |
-type f, d, l | Files, folders, symlinks |
-size +500M, -size -10k | Larger or smaller than |
-mtime -1, -mmin +60 | Modified within a day, or over an hour ago |
-newer ref | Modified after a reference file |
-user www-data, -group dev | Owner and group |
-nouser, -nogroup | Owner ID with no account |
-perm -o+w, -perm /111 | All listed bits set, or any of them |
-empty | Empty files and folders |
-maxdepth 2, -mindepth 1 | Limit recursion depth |
Time units: -mtime counts 24-hour periods, so +30 means more than 30 full days; -mmin counts minutes. -atime and -ctime exist too, but access times are often disabled with noatime mounts.
How do I combine conditions?
Tests next to each other are joined with AND. Use -o for OR, ! or -not for NOT, and escaped parentheses to group:
find /srv/site \( -name '*.bak' -o -name '*.orig' -o -name '*~' \) -type f find /home -type f ! -user root -perm -o+w
Put -maxdepth right after the starting point; GNU find warns if it appears later.
How do I skip folders?
-prune stops find from descending into a folder:
find /srv/app \( -path /srv/app/node_modules -o -path /srv/app/.git \) -prune -o -type f -name '*.js' -print
The pattern reads: if the path is one of these folders, prune it; otherwise, if it is a .js file, print it. The explicit -print at the end is needed, or the pruned folders are printed too.
How do -exec forms differ?
find /srv/uploads -type f -name '*.png' -exec optipng -quiet {} +
find /etc/nginx -name '*.conf' -exec grep -H 'ssl_protocols' {} \;
find /srv/tmp -type f -mtime +7 -ok rm {} \;
{} +passes many paths to one command call: fast, ideal for chmod, chown, grep and similar tools.{} \;runs the command once per file: slower, needed when the command accepts only one file.-okasks before each run: useful the first time you try a destructive command.
For pipelines, use -print0 with xargs -0 so names with spaces or newlines are safe: find . -name '*.log' -print0 | xargs -0 -P 8 gzip compresses with eight parallel processes.
How do I delete safely?
Run the command with -print first, read the list, then replace -print with -delete, keeping everything else identical:
find /var/www/shop/var/cache -type f -mmin +1440 -print | head find /var/www/shop/var/cache -type f -mmin +1440 -delete
-delete must come after the tests. Placed first, it would delete everything find visits before the tests run. More habits for safe deletion are in sudo rm -rf, carefully.
Which admin recipes come up again and again?
find / -xdev -type f -perm -4000 2>/dev/null # setuid programs, for audits
find /srv -xdev -nouser -o -nogroup # orphaned files after user removal
find /etc -type f -newer /var/log/dpkg.log -ls # configs changed after the last package run
find /var/www -type d -exec chmod 755 {} + ; find /var/www -type f -exec chmod 644 {} +
Permission recipes like the last line are covered in chmod recursively. To find files by name only, a faster indexed search is in Find a file on Linux.
Keep Reading
Find a file on Linux
The fastest way to locate a file by name, by package, by command name or by content, from plocate and fd to find and dpkg -S.
Openchmod recursively
Change permissions across whole folder trees without breaking them: chmod -R, the capital X trick and find for files versus folders.
Opensudo rm -rf, carefully
What sudo rm -rf does, the guardrails that exist, the ones that do not, and habits that make deletes recoverable or impossible to misfire.
OpenExclude with grep
Leave things out of grep results: invert matches, skip files and folders by pattern, ignore binaries and comments, and filter noisy log lines.
OpenFAQ
Quick Answers
Use find with -mtime -1, or -mmin -1440 for minutes.
{} + passes many files to one command call; {} \; runs the command once per file.
Use -path with -prune and end the expression with -o and your tests plus -print.
Run with -print first, then swap it for -delete placed after all tests.
Use -print0 with xargs -0, or -exec with {} +.