/flags/find/find-maxdepth-mindepth
find -maxdepth 1: limit how deep find searches (and -mindepth)
- tool
- find
- command
- find
- flag
- -maxdepth N, -mindepth N
- default
- no limit: find descends into every subdirectory (-mindepth 0 includes the starting points)
find . -maxdepth 1 -name '*.log' searches only the starting directory and its direct entries; it does not descend into subdirectories. -maxdepth N stops find from going more than N levels below each starting point, and -mindepth N skips everything shallower than N. The starting point itself is depth 0, so -mindepth 1 excludes . from the results.
Depth in practice
mkdir -p proj/src/lib
touch proj/a.txt proj/src/b.txt proj/src/lib/c.txt
echo '-- maxdepth 1'; find proj -maxdepth 1 | sort
echo '-- maxdepth 1, files only'; find proj -maxdepth 1 -type f | sort
echo '-- exactly depth 2'; find proj -mindepth 2 -maxdepth 2 | sort
echo '-- no starting point'; find proj -mindepth 1 -type d | sortOutput
-- maxdepth 1 proj proj/a.txt proj/src -- maxdepth 1, files only proj/a.txt -- exactly depth 2 proj/src/b.txt proj/src/lib -- no starting point proj/src proj/src/lib
-mindepth 2 -maxdepth 2 selects one exact level, which is a quick way to list, for example, project/*/package.json files without the deeper copies under node_modules.
Values
| Expression | What it returns |
|---|---|
| -maxdepth 0 | Only the starting points themselves; tests are applied to them, nothing is read inside |
| -maxdepth 1 | The starting point and its direct entries (like ls -a, plus the directory) |
| -mindepth 1 | Everything except the starting point |
| -mindepth 1 -maxdepth 1 | Direct entries only, without the starting point |
| -mindepth N -maxdepth N | Exactly level N |
N must be a non-negative integer. -maxdepth also makes find faster on large trees, because find never reads the directories below the limit, while a filter such as -path or grep after the fact still walks the whole tree.
Position matters for warnings, not for results
-maxdepth and -mindepth are global options: they affect the entire expression, wherever they appear. GNU find accepts them after a test but warns that global options are not positional; the warning is shown when warnings are on (by default only when standard input is a terminal). Details and the exact text: warning: you have specified the global option -maxdepth after the argument. Write them first:
find . -maxdepth 2 -type f -name '*.conf'-maxdepth or -prune
-maxdepth limits depth everywhere. To skip particular directories (such as .git or node_modules) at any depth while searching the rest fully, use find -prune instead. The two combine: find . -maxdepth 3 -name .git -prune -o -type f -print.
Platform notes
- GNU find (Linux, Git Bash) and BSD find (macOS, FreeBSD) both support
-maxdepthand-mindepthwith the same meaning. BSD find also has-depth nas a test for an exact depth; in GNU find-depthis unrelated (it processes directory contents before the directory itself). - BusyBox find supports both options as well. Strict POSIX find has neither; use
-prunefor portable scripts.
FAQ
How do I list only the files in the current directory, without subdirectories?
find . -maxdepth 1 -type f. Depth 0 is . itself, depth 1 is its direct entries; -type f drops the directories.
Is -maxdepth POSIX?
No. GNU find and the BSD/macOS find both support it, but POSIX find does not. The portable form is find . ! -name . -prune with the tests you need.
Why does find warn about -maxdepth after -name?
-maxdepth is a global option and applies to the whole expression wherever it is written. GNU find warns when it follows a test; move it right after the starting path.