Searching for Files with find
When a project grows, files get lost in its directory tree. The find command walks through a directory tree and tests every item against conditions you describe, such as its name, type, size or age. It can then print the matches or run another command on each one. Unlike a graphical search, find reads the file system directly, so it works on any directory without an index and gives you precise control over what counts as a match.
Understanding the Concept
The general form of the command is:
find START_DIRECTORY... TESTS ACTIONS
find starts at each start directory, visits every item below it, and evaluates your tests from left to right. When all tests are true for an item, the action runs. If you give no action, find prints the path, which is the same as writing -print.
The examples in this lesson use a small project. It was created with mkdir and touch, and docs/big.dat contains 2,048 bytes:
.
./.env
./.git/config
./README.md
./build/out.o
./docs/Notes.MD
./docs/big.dat
./docs/guide.md
./src/app.py
./src/main.py
./src/utils/helpers.py
./tests/test_app.py
./tests/test_main.py
All the commands below run from the top of this project, which is why the paths begin with ./. The command find . with no tests lists everything. find does not sort its output, so the examples pipe it through sort to make the order predictable.
Finding Files by Name
The test -name matches the file name against a pattern. The pattern must be quoted, so that find receives the asterisk instead of the shell expanding it:
find . -name "*.py" | sort
Output:
./src/app.py
./src/main.py
./src/utils/helpers.py
./tests/test_app.py
./tests/test_main.py
The match applies to the file name only, not to the directory part, and the pattern must match the whole name. -name is case sensitive, so readme.md does not match README.md:
find . -name "readme.md"
find . -iname "readme.md"
Output of the second command (the first prints nothing):
./README.md
The option -iname ignores case. It also lets one pattern catch both .md and .MD files:
find . -iname "*.md" | sort
Output:
./README.md
./docs/Notes.MD
./docs/guide.md
Why Quoting Matters
Suppose you run the search from inside src, where app.py and main.py exist, and you forget the quotes:
cd src
find .. -name *.py
Output:
find: paths must precede expression: `main.py'
find: possible unquoted pattern after predicate `-name'?
The shell expanded *.py into app.py main.py before find started, so find received two words after -name. The message tells you the cause. Quoting the pattern fixes it:
find .. -name "*.py" | sort
Output:
../src/app.py
../src/main.py
../src/utils/helpers.py
../tests/test_app.py
../tests/test_main.py
Finding Files by Type
The test -type selects the kind of item. The two most common values are f for regular files and d for directories:
find . -type d | sort
Output:
.
./.git
./build
./docs
./src
./src/utils
./tests
Combining tests means that all of them must be true, which acts as a logical AND:
find . -type f -name "*.md"
Output:
./README.md
./docs/guide.md
Both files are regular files with names ending in .md. The ./docs/Notes.MD file is missing because -name is case sensitive.
Limiting the Depth
By default find descends through every level. The options -maxdepth and -mindepth restrict the levels it visits. The start directory itself is level 0.
find . -maxdepth 1 -type f | sort
Output:
./.env
./README.md
Only items directly inside the start directory are considered. To see only directories exactly two levels down:
find . -mindepth 2 -maxdepth 2 -type d
Output:
./src/utils
Put these options before other tests. GNU find prints a warning when a depth option comes after a test, because the order can matter.
Combining Conditions
Negation and Alternatives
Use ! or -not to invert a test, and -o for "or". Parentheses group tests, and since they have a meaning to the shell, they must be escaped with a backslash:
find . -type f -not -name "*.py" | sort
Output:
./.env
./.git/config
./README.md
./build/out.o
./docs/Notes.MD
./docs/big.dat
./docs/guide.md
find . \( -name "*.md" -o -name "*.MD" \) | sort
Output:
./README.md
./docs/Notes.MD
./docs/guide.md
Without the parentheses, a command such as find src docs -name "*.md" -o -name "*.py" still works for a plain print, but it breaks as soon as you add an action, because -o has lower priority than the implied AND. Grouping with