The Linux find command searches a directory tree for files that match tests such as name, type, size or modification time: find . -name "*.log" lists every .log file under the current directory. The general form is find [where to look] [tests] [action], and the default action is to print each match.
find /var/log -type f -name "*.log" -size +10M
That finds regular files ending in .log larger than 10 MiB under /var/log. Tests listed one after another must all be true. All output below is from GNU findutils 4.10.0, the version used on most Linux distributions; macOS ships BSD find, which supports the common tests here but not every GNU extension.
Find files by name
find . -name "*.txt" # case-sensitive
find . -iname "*.py" # case-insensitive: matches main.py and util.PY
find /etc -name "nginx.conf" # exact name
find . -name "*report*" # name contains "report"
-name matches only the file name, not the directory path. To match the path, use -path, for example find . -path "*/tests/*.py".
Fix: find: paths must precede expression
This is the error you get when you forget the quotes:
$ find . -name *.txt
find: paths must precede expression: `b.txt'
find: possible unquoted pattern after predicate `-name'?
The shell expanded *.txt into a.txt b.txt before find ran, so find received an extra word it doesn't understand. Quote the pattern ("*.txt" or '*.txt') so the wildcard reaches find intact. The same error appears if you put the starting path after the tests, as in find -name x project: the path always comes first.
Find only files or only directories
find . -type f # regular files
find . -type d # directories
find . -type l # symbolic links
find . -type f,l # files or links (GNU extension)
Find files by size
find . -type f -size +100M # larger than 100 MiB
find . -type f -size -10k # smaller than 10 KiB
find . -type f -size 0 # exactly zero bytes
find . -empty # empty files and empty directories
Units: c bytes, k KiB, M MiB, G GiB. With no unit, the number counts 512-byte blocks, which surprises people. + means more than and - means less than.
One trap: sizes are rounded up to the unit before comparing. A 300-byte file counts as 1k, so -size -1k means "less than 1 KiB after rounding up", which only matches empty files. The manual spells it out: -size -1M is not the same as -size -1048576c. When precision matters, use bytes: -size -1048576c.
Find files by modification time
find . -mtime -1 # modified in the last 24 hours
find . -mtime +30 # modified more than 30 days ago
find . -mmin -15 # modified in the last 15 minutes
find . -newer build.log # modified more recently than build.log
-mtime counts whole 24-hour periods and throws away the fraction. So -mtime +30 really means at least 31 full days ago, and -mtime 0 means within the last 24 hours. For finer control use -mmin, which counts minutes. -atime and -ctime work the same way for access time and status-change time.
Limit how deep find searches
$ find project -maxdepth 1
project
project/logs
project/node_modules
project/README.md
project/src
-maxdepth 1 looks only at the starting directory's direct contents. -mindepth 1 skips the starting directory itself. They apply to the whole search wherever you place them, so put them right after the path where readers expect them.
Skip a directory with -prune
To search a project but ignore node_modules:
$ find project -path project/node_modules -prune -o -type f -print
project/logs/big.log
project/logs/empty.log
project/README.md
project/src/main.py
project/src/util.PY
Read it as: "if the path is project/node_modules, prune it (don't descend); otherwise, if it is a file, print it." The explicit -print at the end matters. Without it, find's default print applies to the whole expression and the pruned directory's own name is printed too.
Combine tests with OR, NOT and parentheses
find . -name "*.jpg" -o -name "*.png" # either extension
find . -type f ! -name "*.md" # NOT a .md file
find . -type f \( -name "*.jpg" -o -name "*.png" \) -size +1M
AND binds tighter than -o (OR). Without parentheses, -type f -name "*.jpg" -o -name "*.png" means "(files named *.jpg) or (anything named *.png)". The parentheses must be escaped or quoted so the shell passes them to find.
Run a command on each result with -exec
find . -name "*.log" -exec ls -l {} + # one ls for many files
find . -name "*.sh" -exec chmod +x {} \; # one chmod per file
find . -name "*.py" -exec grep -l "TODO" {} + # Python files containing TODO
{} is replaced by the file name. Ending with \; runs the command once per file; ending with {} + passes as many names as possible to each run, which is much faster for large result sets. -execdir works like -exec but runs the command from the directory containing each file, which the manual describes as the safer choice.
For file names with spaces or newlines, the safe pipe form is:
find . -type f -print0 | xargs -0 wc -l
-print0 separates names with a null byte and xargs -0 splits on it, so no name can be misread.
Delete files with find (safely)
find /tmp/build -name "*.o" -type f -print # 1. check what matches
find /tmp/build -name "*.o" -type f -delete # 2. then delete
Always run the command with -print first and read the list. -delete is evaluated in order like any other action, so it must come last. The manual warns that putting -delete first makes find try to delete everything below the starting points. -delete also implies -depth (contents before their directory), which is why it cannot be combined with -prune. GNU find refuses with find: The -delete action automatically turns on -depth, but -prune does nothing when -depth is in effect.
Find by owner or permissions
find /srv -user alex # owned by user alex
find . -type f -perm 644 # permissions exactly 644
find . -type f -perm -o+w # world-writable (that bit is set)
find . -type f -perm /u+x,g+x # executable by owner OR group
A bare mode means exactly that mode; -mode means all listed bits are set; /mode means any of them is set. Linux file permissions explained covers what the numbers mean.
Quick reference
| Goal | Command |
|---|---|
| Largest files under the current folder | find . -type f -size +500M -exec ls -lh {} + |
| Empty directories | find . -type d -empty |
| Files changed today | find . -type f -mtime 0 |
| Logs older than 14 days, deleted | find /var/log/myapp -name "*.log" -mtime +14 -delete |
| Search all of / without errors | find / -name "nginx.conf" 2>/dev/null |
For disk-space hunting specifically, how to find large files combines find with du. For the other everyday commands, see basic Linux commands.
If find returns nothing
Check case first: -name is case-sensitive, so Report.PDF won't match "*.pdf"; use -iname. Then check that the starting path is where you think it is, and that you aren't filtering out your target with a stray -type or -maxdepth. Directories you can't read print Permission denied and are skipped, so a file there won't appear unless you run the search with sudo.
Frequently asked questions
- What is the difference between find and locate?
- find walks the directory tree every time you run it, so its results are always current but it can be slow on large trees. locate searches a prebuilt database of file names, so it is nearly instant but only knows about files that existed when its database was last rebuilt (with updatedb).
- How do I hide Permission denied errors from find?
- Redirect standard error to /dev/null: find / -name nginx.conf 2>/dev/null. This hides all error messages, including real ones, so use it only when you know the errors are just directories you are not allowed to read. Running the search with sudo is the other option.
- How do I find files containing a word, not files with a name?
- Use grep instead: grep -rl 'word' /path lists files whose contents contain the word. You can combine the two: find . -name '*.py' -exec grep -l 'word' {} + searches only Python files.
- Does find follow symbolic links?
- Not by default. GNU find treats a symbolic link as the link itself and does not descend into a linked directory. Add -L before the starting path, as in find -L /path -name x, to follow links.