Understanding Depth In Linux Find Command

Updated: Mar 4

Find command on Linux is a very powerful tool to search files or directories. We can use maxdepth/mindepth to limit down the search to specific depth levels.


How to Use Depth In Find Command

We can combine two options together to limit the search only between max/min depths. On Centos, there is no option for -depth. We can use -maxdepth n -mindepth n to search files or directories only under n depth level.


Learning Maxdepth and mindepth

The descriptions of maxdepth and mindepth are in the following:

  • maxdepth levels - Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

  • mindepth levels - Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments.



Examples of Depth in Find Command

We can use the following examples to limit the depth level in Linux find command.

# find /etc -maxdepth 2 -name passwd
/etc/passwd
/etc/pam.d/passwd
# find /etc -mindepth 1 -name passwd
/etc/passwd
/etc/pam.d/passwd
# find /etc -mindepth 1 -maxdepth 1 -name passwd
/etc/passwd
# find /etc -mindepth 1 -maxdepth 2 -name passwd
/etc/passwd
/etc/pam.d/passwd

Related Post:

Linux Find Exec examples - Advanced Part

5 Examples for Linux Find command


712 views

Join our newsletter. Get a free Linux account on Cloud.

Get a Free Cloud Server! 

We can use this cloud server to practice Linux commands. Never miss a post!

Thanks for submitting!