← Full reference

find

Find files by name, type, size, modification time and permissions

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

OptionWhat it does
-maxdepth Ndon't descend deeper than N levels (0 — only the starting paths themselves, 1 — their contents)
-mindepth Ndon't test files closer than N levels (-mindepth 1 excludes the starting directory itself)
-xdevdon't cross into other filesystems (/proc, mounted volumes)
-depthprocess a directory's contents first, then the directory itself
-P / -L / -Hdon't follow symlinks (default) / follow all / follow only those given on the command line
-regextype posix-extendedregular 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

ConditionMeaning
-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

ConditionMeaning
-type f / d / lregular file / directory / symbolic link
-type p / s / b / cnamed pipe / socket / block / character device
-type f,dany of the listed types (GNU)
-xtype la link whose target doesn't exist ("dangling")
-emptyempty file or empty directory

Conditions: size and time

ConditionMeaning
-size +10M / -size -1klarger 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 0modified more than 7 full days ago / less than a day ago / within the last 24 hours
-mmin +30modified more than 30 minutes ago; -mmin -5 — within the last 5 minutes
-atime, -aminsame, by last access time
-ctime, -cminsame, by metadata change time (permissions, owner, rename)
-newer filemodified later than the given file
-newermt '2024-06-01 12:00'modified later than the given date; ! -newermt … — not later
-daystartcount days from the start of today rather than from now (put it before -mtime)

Conditions: owner, permissions, links

ConditionMeaning
-user name / -group nameowner / group (a number works too)
-uid N / -gid Nnumeric UID / GID
-nouser / -nogroupthe file's UID/GID doesn't exist on the system
-perm 644permissions exactly 644
-perm -u+x or -perm -0111all of the given bits are set (others may be too)
-perm /u+s,g+s or -perm /6000at least one of the given bits is set
-readable / -writable / -executablethe current user can read / write / execute
-links Nnumber of hard links; -links +1 — the file has other names
-inum N / -samefile fileby inode number / all names of the same file

Operators

OperatorMeaning
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

ActionWhat it does
-printprint the path (default action)
-print0print paths separated by a null byte — pairs with xargs -0
-printf 'format'print using a template (see below); add the newline \n explicitly
-lsdetailed line, like ls -dils
-fprint filewrite paths to a file instead of the screen
-deletedelete 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
-prunedon't descend into this directory
-quitstop 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

DirectiveWhat it prints
%p / %Ppath / path without the starting directory
%f / %hfile name / the directory it lives in
%s / %ksize in bytes / space used in KiB
%m / %Mpermissions as a number (644) / as letters (-rw-r--r--)
%u / %gowner / group
%TY-%Tm-%Td %TH:%TMmodification date in parts; %t — all at once, %T@ — seconds since 1970
%y / %d / %ntype (f, d, l) / depth / number of hard links
%lwhere 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