Skip to content
Latchkey

find -maxdepth: Limit How Deep find Walks

find -maxdepth limits how many directory levels deep find descends.

A bounded search is faster and safer. -maxdepth 1 keeps find in the current directory instead of walking the whole tree.

What it does

find -maxdepth N descends at most N levels below each starting point; the starting point itself is depth 0. find -mindepth N skips matches shallower than N levels. They bound the walk so it does not recurse forever or report the start directory.

Common usage

Terminal
find . -maxdepth 1 -type f -name '*.yml'
find . -mindepth 1 -maxdepth 1 -type d      # immediate subdirs only
find logs -maxdepth 2 -name '*.log'

Options

OptionWhat it does
-maxdepth NDescend at most N levels (start = depth 0)
-mindepth NIgnore matches shallower than N levels
-maxdepth 1Search only the immediate directory contents
-mindepth 1Exclude the starting directory itself

In CI

These are global options and GNU find warns if they appear after a test. Put -maxdepth and -mindepth right after the path and before predicates like -name to avoid the warning and surprising results.

Common errors in CI

"find: warning: you have specified the global option -maxdepth after the argument -name, but global options are not positional" (GNU) means you wrote the test first; move -maxdepth ahead of it. -maxdepth and -mindepth are GNU extensions but are also supported by BSD/macOS find, so they are portable in practice.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →