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.
| Part | Role | Examples |
|---|---|---|
| Starting points | Directories to search | ., /var/log, /home /srv |
| Global options | Change traversal behavior | -maxdepth 2, -mindepth 1, -xdev |
| Tests | Return true or false per entry | -name, -type, -mtime, -size |
| Actions | Do something with matches | -print, -delete, -exec |
| Operators | Combine tests and actions | -a, -o, !, \( \) |
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.
| Test | Matches | Example |
|---|---|---|
| -name / -iname | Base name by shell glob, case-insensitive with -iname | -name '*.log' |
| -path | Whole path by glob | -path '*/cache/*' |
| -type | File type such as f, d or l | -type f |
| -mtime / -mmin | Modification age in days or minutes | -mtime +30 |
| -size | Size in units such as k, M or G | -size +100M |
| -user / -group | Owner or group | -user www-data |
| -perm | Permission bits, exact or partial | -perm -o+w |
| -newer | Modified more recently than a reference file | -newer stamp.txt |
| -empty | Empty files or directories | -type d -empty |
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.
| Method | Behavior | Best for |
|---|---|---|
| -exec cmd {} \; | One process per file | Commands that accept only one argument |
| -exec cmd {} + | Batches many paths per process | Bulk operations such as chmod |
| -execdir | Runs from the file’s own directory | Safer handling of untrusted trees |
| -ok | Asks for confirmation per file | Careful interactive changes |
| -print0 with xargs -0 | Null-separated paths through a pipe | Parallel processing with xargs -P |
| -delete | Removes matches directly | Cleanup of temporary 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 compared with related tools
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.
| Tool | Data source | Strengths | Weaknesses |
|---|---|---|---|
| find | Live directory traversal | Any metadata test, actions | Slow on huge trees |
| locate / plocate | Prebuilt name index | Near-instant name search | Index can be outdated |
| fd | Live traversal, parallel | Fast, simple syntax, ignores .gitignore paths | Not installed by default, not POSIX |
| grep -r | File contents | Searches inside files | No metadata selection |
| ls with globbing | Single directory | Quick and familiar | No recursion or rich tests |
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' -printprints only header files, because -print binds to the second test. - Permission denied noise: redirect errors with
2>/dev/nullor restrict the starting point instead of running as root. - Crossing file systems:
-xdevkeeps the search on one file system and avoids scanning network mounts or /proc. - Following symbolic links: the default
-Pdoes not follow links;-Lfollows them and can create loops. - Portability:
-printf,-delete,-inameand-maxdepthare 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.