find walks a directory tree and checks every file against the conditions you give it. Matching files are printed or passed to a command. Unlike ls and shell globs, find descends into every subdirectory and can select by any file property.
Syntax
find [-H|-L|-P] [where to search…] [traversal options] [conditions] [action]
If no path is given, it searches the current directory. Conditions are read left to right and are joined with "and" by default. Without an action, find just prints the paths (-print).
Traversal options
| Option | What it does |
|---|
-maxdepth N | don't descend deeper than N levels (0 — only the starting paths themselves, 1 — their contents) |
-mindepth N | don't test files closer than N levels (-mindepth 1 excludes the starting directory itself) |
-xdev | don't cross into other filesystems (/proc, mounted volumes) |
-depth | process a directory's contents first, then the directory itself |
-P / -L / -H | don't follow symlinks (default) / follow all / follow only those given on the command line |
-regextype posix-extended | regular expression syntax for -regex |
-maxdepth, -mindepth, -xdev and -depth apply to the whole expression wherever they appear. Put them right after the path — that reads unambiguously.
Conditions: name and path
| Condition | Meaning |
|---|
-name 'pattern' | file name (without directory) matched by a shell pattern: *, ?, [abc]; -iname — case-insensitive |
-path 'pattern' | the whole path as find prints it, e.g. './src/*/test/*'; -ipath — case-insensitive |
-regex 'expression' | regular expression against the whole path; -iregex — case-insensitive |
-lname 'pattern' | a symbolic link pointing to a path matching the pattern |
Conditions: file type
| Condition | Meaning |
|---|
-type f / d / l | regular file / directory / symbolic link |
-type p / s / b / c | named pipe / socket / block / character device |
-type f,d | any of the listed types (GNU) |
-xtype l | a link whose target doesn't exist ("dangling") |
-empty | empty file or empty directory |
Conditions: size and time
| Condition | Meaning |
|---|
-size +10M / -size -1k | larger than 10 MiB / smaller than 1 KiB; units c (bytes), k, M, G; the size is rounded up to the unit |
-mtime +7 / -mtime -1 / -mtime 0 | modified more than 7 full days ago / less than a day ago / within the last 24 hours |
-mmin +30 | modified more than 30 minutes ago; -mmin -5 — within the last 5 minutes |
-atime, -amin | same, by last access time |
-ctime, -cmin | same, by metadata change time (permissions, owner, rename) |
-newer file | modified later than the given file |
-newermt '2024-06-01 12:00' | modified later than the given date; ! -newermt … — not later |
-daystart | count days from the start of today rather than from now (put it before -mtime) |
Conditions: owner, permissions, links
| Condition | Meaning |
|---|
-user name / -group name | owner / group (a number works too) |
-uid N / -gid N | numeric UID / GID |
-nouser / -nogroup | the file's UID/GID doesn't exist on the system |
-perm 644 | permissions exactly 644 |
-perm -u+x or -perm -0111 | all of the given bits are set (others may be too) |
-perm /u+s,g+s or -perm /6000 | at least one of the given bits is set |
-readable / -writable / -executable | the current user can read / write / execute |
-links N | number of hard links; -links +1 — the file has other names |
-inum N / -samefile file | by inode number / all names of the same file |
Operators
| Operator | Meaning |
|---|
A B or A -a B | "and" (-and) |
A -o B | "or" (-or); "and" has higher precedence |
! A or -not A | "not" |
\( … \) | grouping; escape the parentheses from the shell and separate them with spaces |
Actions
| Action | What it does |
|---|
-print | print the path (default action) |
-print0 | print paths separated by a null byte — pairs with xargs -0 |
-printf 'format' | print using a template (see below); add the newline \n explicitly |
-ls | detailed line, like ls -dils |
-fprint file | write paths to a file instead of the screen |
-delete | delete matches; directories only if empty |
-exec command {} \; | run the command for each file; {} is the path |
-exec command {} + | run the command once per batch of files — faster; {} must come last |
-execdir command {} \; | same, but run in the matched file's directory |
-ok command {} \; | like -exec, but asks for confirmation for each file |
-prune | don't descend into this directory |
-quit | stop right after the first match |
-exec … \; also works as a condition: it is true if the command returns 0. This lets you filter by any test: -exec grep -q text {} \; -print.
-printf and -ls format
| Directive | What it prints |
|---|
%p / %P | path / path without the starting directory |
%f / %h | file name / the directory it lives in |
%s / %k | size in bytes / space used in KiB |
%m / %M | permissions as a number (644) / as letters (-rw-r--r--) |
%u / %g | owner / group |
%TY-%Tm-%Td %TH:%TM | modification date in parts; %t — all at once, %T@ — seconds since 1970 |
%y / %d / %n | type (f, d, l) / depth / number of hard links |
%l | where a symbolic link points |
An -ls line: inode number, space used in KiB, permissions, link count, owner, group, size in bytes, modification date, path.
Examples
# all .conf in /etc, regular files only
find /etc -type f -name '*.conf'
# files over 100 MiB on the root filesystem, access errors go to /dev/null
find / -xdev -type f -size +100M 2>/dev/null
# logs older than 7 days
find /var/log -name '*.log' -mtime +7
# modified in the last 30 minutes
find ~/project -type f -mmin -30
# modified within a date range
find /srv/app -newermt '2024-01-01' ! -newermt '2024-02-01'
# empty directories in the current directory, without descending into nested ones
find . -maxdepth 1 -type d -empty
# .log OR .tmp — escape the grouping parentheses from the shell
find . \( -name '*.log' -o -name '*.tmp' \) -type f
# skip node_modules and .git
find . \( -name node_modules -o -name .git \) -prune -o -type f -print
# files with the setuid bit across the system
find / -xdev -type f -perm -4000 2>/dev/null
# files whose owner has no account
find /home -nouser
# size, permissions and name in a custom format, largest first
find /var/log -type f -printf '%s\t%m\t%p\n' | sort -rn | head
# count lines in all .py files with a single wc call
find . -name '*.py' -exec wc -l {} +
# keep only files that contain a string
find /etc -name '*.conf' -exec grep -q 'include' {} \; -print
# safely pass names with spaces to xargs
find . -name '*.bak' -print0 | xargs -0 rm --
# delete old temp files (check without -delete first)
find /tmp -type f -name '*.tmp' -mtime +3 -delete
# first match — and that's it
find / -name 'nginx.conf' -print -quit 2>/dev/null
Pitfalls
- An unquoted pattern is expanded by the shell before
find runs. With one .log file in the current directory, find . -name *.log becomes -name app.log and finds only files with that name; with several, it breaks ("paths must precede expression"). Always quote the pattern.
-name looks only at the name. -name 'src/*.js' never matches — use -path for paths. -regex, on the other hand, is matched against the whole path: -regex '.*\.log', not -regex '\.log'.
-mtime +7 means "more than 7 full days", i.e. effectively 8 or more: the fractional part is discarded. -mtime 7 means 7 to 8 days ago.
- Size is rounded up.
-size -1M finds only empty files: a 1-byte file already counts as "1 megabyte". For exact thresholds use c (bytes) or k.
- Order matters.
-delete at the start of the expression deletes everything before the other conditions are checked. Run without -delete first and review the list.
-delete and -prune don't mix: -delete implies -depth, under which -prune doesn't work, and find refuses to run. -delete doesn't remove non-empty directories ("Directory not empty").
-o without parentheses. find . -name '*.log' -o -name '*.tmp' -print prints only .tmp: -print binds to the second half. Wrap the "or" in \( … \).
-exec … {} +: {} must come right before +, otherwise you get "missing argument to `-exec'".
- Exit code. "Nothing found" is success (code 0). A non-zero code means errors: missing directory, no access. Check the output, not the code.
- Names with spaces and newlines break
find … | xargs. Use -print0 with xargs -0, or -exec … {} +.
See also
xargs · ls · stat · chmod · grep · tar · df-du