find · Linux command

Also known as: find command

On this page 7

find is a Linux command that recursively walks a directory tree and selects files and directories that match a set of tests, such as name, type, size, modification time, owner or permissions. In the Linux shell & terminal environment, the find command is the standard tool for locating files by their attributes and for running an action on every match, from printing a path to deleting a file or passing it to another program.

The command is defined by POSIX and is available on every Unix-like system; most Linux distributions ship GNU findutils, which adds many extensions. Unlike grep, which searches inside file contents, find works on file system metadata, and it combines naturally with xargs, pipes and shell scripts.

How find evaluates an expression

The general form is find [starting-point...] [expression]. find descends into each starting point, visits every entry and evaluates the expression from left to right for that entry. The expression consists of options, tests, actions and operators, and evaluation stops early when the result is already known, exactly like short-circuit logic in programming.

Adjacent tests are joined by an implicit AND, written explicitly as -a; -o means OR and ! or -not negates a test. Parentheses group terms but must be escaped or quoted as \( and \) so that the shell does not interpret them. If no action is given, find applies -print by default to every entry for which the expression is true.

PartRoleExamples
Starting pointsDirectories to search., /var/log, /home /srv
Global optionsChange traversal behavior-maxdepth 2, -mindepth 1, -xdev
TestsReturn true or false per entry-name, -type, -mtime, -size
ActionsDo something with matches-print, -delete, -exec
OperatorsCombine tests and actions-a, -o, !, \( \)
Parts of a find expression

Because evaluation is ordered, placement matters. In find . -name '*.tmp' -delete, only matching files are deleted, while find . -delete -name '*.tmp' would delete everything before the name test runs. GNU find also warns when global options such as -maxdepth appear after tests.

Common tests and their meaning

Tests select entries by name, type, time, size, ownership and permissions. Numeric arguments follow a shared convention: +n means greater than n, -n means less than n, and a plain n means exactly n, after rounding into the unit of the test.

TestMatchesExample
-name / -inameBase name by shell glob, case-insensitive with -iname-name '*.log'
-pathWhole path by glob-path '*/cache/*'
-typeFile type such as f, d or l-type f
-mtime / -mminModification age in days or minutes-mtime +30
-sizeSize in units such as k, M or G-size +100M
-user / -groupOwner or group-user www-data
-permPermission bits, exact or partial-perm -o+w
-newerModified more recently than a reference file-newer stamp.txt
-emptyEmpty files or directories-type d -empty
Frequently used find tests

Patterns for -name must be quoted, as in -name '*.conf'. Without quotes, the shell may expand the glob against the current directory before find receives it, which produces confusing errors or wrong results. Time tests count in whole 24-hour periods, so -mtime +1 actually means at least two days old, a frequent source of off-by-one surprises.

Actions and running commands on matches

Actions decide what happens to each match. Besides -print, GNU find offers -printf for custom formatting, -ls for a long listing, -delete for removal and -prune for skipping whole subtrees, which is the efficient way to exclude directories such as .git or node_modules.

The -exec action runs an external command. With the ; terminator, written as \;, the command runs once per file; with the + terminator, find appends as many paths as fit on one command line, similar to xargs, which is far faster for large result sets.

MethodBehaviorBest for
-exec cmd {} \;One process per fileCommands that accept only one argument
-exec cmd {} +Batches many paths per processBulk operations such as chmod
-execdirRuns from the file’s own directorySafer handling of untrusted trees
-okAsks for confirmation per fileCareful interactive changes
-print0 with xargs -0Null-separated paths through a pipeParallel processing with xargs -P
-deleteRemoves matches directlyCleanup of temporary files
Ways to process matching files

File names may contain spaces, quotes or even newlines. Null-separated output with -print0 and xargs -0, or -exec ... +, handles all of them safely, whereas piping plain -print output into xargs breaks on whitespace.

find is the most flexible but not always the fastest way to locate files. Other tools trade flexibility for speed or convenience, and choosing correctly saves time on large file systems.

ToolData sourceStrengthsWeaknesses
findLive directory traversalAny metadata test, actionsSlow on huge trees
locate / plocatePrebuilt name indexNear-instant name searchIndex can be outdated
fdLive traversal, parallelFast, simple syntax, ignores .gitignore pathsNot installed by default, not POSIX
grep -rFile contentsSearches inside filesNo metadata selection
ls with globbingSingle directoryQuick and familiarNo recursion or rich tests
find and alternative file search tools

Pitfalls and safe usage

Most problems with find come from operator precedence, unquoted patterns and destructive actions tested too late. A reliable habit is to run the command with -print first and replace it with -delete or -exec only after reviewing the output.

  • Missing parentheses with -o: find . -name '*.c' -o -name '*.h' -print prints only header files, because -print binds to the second test.
  • Permission denied noise: redirect errors with 2>/dev/null or restrict the starting point instead of running as root.
  • Crossing file systems: -xdev keeps the search on one file system and avoids scanning network mounts or /proc.
  • Following symbolic links: the default -P does not follow links; -L follows them and can create loops.
  • Portability: -printf, -delete, -iname and -maxdepth are GNU or BSD extensions, so POSIX scripts should avoid them.

The idea in brief

find is like a very patient assistant who walks through every folder on a computer. It writes down each file that fits your description. You might ask for all photos larger than 10 megabytes. You might ask for all files nobody has touched for a month.

The assistant can also do a job with each file it finds. It can move a file, change who may open it or delete it. That power is useful but deserves care. It is wise to look at the list first and only then change or remove files.

A practical scenario

A system administrator receives an alert that the root file system of a Linux web server is 92 percent full. To see what is consuming space, the administrator runs find /var -xdev -type f -size +500M -printf '%s %p\n' and discovers several multi-gigabyte log archives that rotation had failed to remove.

Next, the administrator lists compressed logs older than 30 days with find /var/log/app -type f -name '*.gz' -mtime +30 -print, reviews the 412 paths, and only then replaces -print with -delete. Finally, a nightly cron job runs the same command so the problem does not return.

The cleanup frees about 38 gigabytes in a few minutes without restarting any service, and the scheduled job keeps disk usage below 60 percent in the following months.