Skip to Content

Understanding Maxdepth Mindepth Depth In Linux Find Command

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.

 

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

10 Advanced Find Exec examples in Linux

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

6 ways to Find Files By Name in Linux

Related Post:

Linux Find Exec examples – Advanced Part

5 Examples for Linux Find command

Find File By Name in Linux

20 Advanced Linux Find Command Examples

How to use Find Command in Linux

3 Ways to find the largest files in Linux

10 Linux Find Exec examples – Advanced Part