How-to

The find Command, Fully Explained

find walks a folder tree and applies tests and actions to every entry. Once its expression syntax clicks, it replaces many small scripts.

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?

TestMatches
-name '*.php', -inameBase name, case-sensitive or not
-path '*/cache/*'Whole path pattern
-type f, d, lFiles, folders, symlinks
-size +500M, -size -10kLarger or smaller than
-mtime -1, -mmin +60Modified within a day, or over an hour ago
-newer refModified after a reference file
-user www-data, -group devOwner and group
-nouser, -nogroupOwner ID with no account
-perm -o+w, -perm /111All listed bits set, or any of them
-emptyEmpty files and folders
-maxdepth 2, -mindepth 1Limit 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.
  • -ok asks 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.

Open

chmod recursively

Change permissions across whole folder trees without breaking them: chmod -R, the capital X trick and find for files versus folders.

Open

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

Open

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

Open

FAQ

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 {} +.