Understanding Depth In Linux Find Command

Updated: Feb 7

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


212 views

Join our mail group. Get a free Linux account on Cloud.

Never miss a post!

Want a free Linux account?  This account can be used to login to our cloud server and practice Linux commands.

 
kamateravps.gif
topbackgroud.png

✔ Linux Commands   ✔ Linux Skills    ✔ LinuxPerformance   ✔ Linux Interview

Some articles are from the public internet. If you find your article misused or undesired here and you don't want us to display it, please let us know and we'll remove it immediately.