TExcelTAD
Home › Linux & Bash › find: Locate Files

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:

bash · try it yourself
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 | sort
shop
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
bash · try it yourself
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 *.py itself (if .py files exist in the current directory) before find ever sees it.

By type

-type f selects regular files, -type d directories, -type l symbolic links:

bash · try it yourself
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 l
project
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
bash · try it yourself
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 marker
logs/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:

bash · try it yourself
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 -l
cache/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).
  • -delete removes matches. Run the same command without -delete first 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:

bash · try it yourself
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 f
3 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?

  1. find src -name d
  2. find src -type d
  3. find -d src
  4. ls -d src

Why should you quote '*.log' in find . -name '*.log'?

  1. find requires quotes
  2. So the shell doesn't expand the glob before find sees it
  3. To make the search case-insensitive
  4. To search hidden files

What is the difference between -exec cmd {} \; and -exec cmd {} +?

  1. None
  2. \; runs cmd once per file, + passes many files to one call
  3. + deletes files
  4. \; only works on directories