find: Locate Files
find walks a directory tree and prints every file that matches your conditions: by name, type, size, age, permissions and more. It can also run a command on each result, which makes it one of the most powerful tools on Linux.
The general form is find WHERE CONDITIONS ACTIONS. With no conditions it lists everything:
mkdir -p shop/{src,tests,docs}
touch shop/src/{cart,checkout}.py shop/tests/test_cart.py shop/docs/README.md shop/notes.txt
find shop | sortshop
shop/docs
shop/docs/README.md
shop/notes.txt
shop/src
shop/src/cart.py
shop/src/checkout.py
shop/tests
shop/tests/test_cart.py
The order find visits files in depends on the filesystem, so the examples pipe into sort to get a stable listing.
By name
| Test | Matches |
|---|---|
-name 'PATTERN' |
File name, case-sensitive, with glob wildcards * ? [] |
-iname 'PATTERN' |
Same, ignoring case |
-path 'PATTERN' |
The whole path |
mkdir -p shop/{src,tests,docs}
touch shop/src/{cart,checkout}.py shop/tests/test_cart.py shop/docs/README.md shop/notes.txt
find shop -name '*.py' | sort
echo "---"
find shop -iname 'readme*'
echo "---"
find shop -name 'test_*'shop/src/cart.py
shop/src/checkout.py
shop/tests/test_cart.py
---
shop/docs/README.md
---
shop/tests/test_cart.py
Note: Always quote the pattern. Unquoted, the shell may expand
*.pyitself (if.pyfiles exist in the current directory) beforefindever sees it.
By type
-type f selects regular files, -type d directories, -type l symbolic links:
mkdir -p project/{lib,bin}
touch project/lib/util.sh project/bin/run
ln -s ../lib/util.sh project/bin/util
find project -type d | sort
echo "---"
find project -type f | sort
echo "---"
find project -type lproject
project/bin
project/lib
---
project/bin/run
project/lib/util.sh
---
project/bin/util
By size, age and permissions
| Test | Meaning |
|---|---|
-size +10M |
Bigger than 10 MiB (-size -1k: smaller than 1 KiB, c = bytes) |
-empty |
Empty files or directories |
-mtime -7 |
Modified less than 7 days ago (+30: more than 30 days) |
-mmin -60 |
Modified less than 60 minutes ago |
-newer ref |
Modified more recently than file ref |
-perm -u+x |
User-executable |
-maxdepth n |
Don't descend more than n levels |
mkdir logs
printf '%2000s' x > logs/big.log
printf 'small\n' > logs/small.log
touch logs/empty.log
echo 'archived' > logs/old.log
touch -d '2026-01-01' logs/old.log
touch -d '2026-02-01' marker
find logs -type f -size +1k
find logs -empty
find logs -type f ! -newer markerlogs/big.log
logs/empty.log
logs/old.log
Without -type f, the size test would also match the logs directory itself (a directory entry takes 4 KiB on most filesystems). ! (or -not) negates a test: "files not newer than marker". Conditions listed one after another must all match; use -o for "or", with escaped parentheses for grouping: \( -name '*.jpg' -o -name '*.png' \).
Actions: -delete and -exec
By default find prints the matches (-print). Other actions:
mkdir -p cache/{a,b}
touch cache/a/1.tmp cache/b/2.tmp cache/b/keep.dat
find cache -name '*.tmp' -delete
find cache -type f
echo "---"
printf 'line\nline\n' > cache/a/x.txt
printf 'line\n' > cache/b/y.txt
find cache -name '*.txt' -exec wc -l {} \; | sort
find cache -name '*.txt' -exec cat {} + | wc -lcache/b/keep.dat
---
1 cache/b/y.txt
2 cache/a/x.txt
3
-exec CMD {} \;runs the command once per file, with{}replaced by the path.-exec CMD {} +passes many paths to one command call (faster).-deleteremoves matches. Run the same command without-deletefirst to check what it would remove!
Formatting the output
GNU find's -printf prints chosen details for each file: %p path, %f name, %s size, %d depth:
mkdir -p music/jazz
printf 'abc' > music/jazz/track1.mp3
printf 'abcdefgh' > music/track2.mp3
find music -type f -printf '%s bytes depth %d %f\n' | sort -n
find music -maxdepth 1 -type f3 bytes depth 2 track1.mp3
8 bytes depth 1 track2.mp3
music/track2.mp3
find vs locate
locate searches a prebuilt database of file names and is instant, but the database is updated only periodically (updatedb). find always looks at the real filesystem right now.
Test yourself
Which command finds all directories under src?
- find src -name d
- find src -type d
- find -d src
- ls -d src
Why should you quote '*.log' in find . -name '*.log'?
- find requires quotes
- So the shell doesn't expand the glob before find sees it
- To make the search case-insensitive
- To search hidden files
What is the difference between -exec cmd {} \; and -exec cmd {} +?
- None
\;runs cmd once per file,+passes many files to one call+deletes files\;only works on directories