Home Linux Commands How To Use maxdepth and mindepth Options With Linux find command

How To Use maxdepth and mindepth Options With Linux find command

Searching for Files and Directories within Specified Range in Linux: A Beginners Guide

By sk
Published: Updated: 1.8K views

In this tutorial, let's learn how to use the maxdepth and mindepth options with the find command to search for files and directories within specified range i.e. depth levels in Linux with examples.

At the end, we also provide a simple analogy to help newbies easily understand the mindepth and maxdepth options of the Linux find command.

What are maxdepth and mindepth Options in find Command?

For those who don't know yet, the maxdepth option in find command specifies the maximum depth (number of levels) to descend into the directory hierarchy. It limits the search to a specific depth level or fewer.

The mindepth option specifies the minimum depth (number of levels) at which the search should start applying tests or actions. It ensures that the search does not include files or directories at shallower levels.

The maxdepth and mindepth options allow you to limit the depth of directory traversal, which can be useful when you want to focus on specific levels of the directory tree.

Let us see some examples on how to use these two options to effectively search for files and directories from command line in a Linux system.

Search for Files and Directories within Specified Range using find Command's maxdepth and mindepth Options

For demonstration purpose, we will be using the following directory.

$ tree MyFiles/
MyFiles/
├── Dir1
│   ├── Dir2
│   │   ├── Dir3
│   │   │   └── file3
│   │   └── file2
│   ├── file1
│   └── file1.1
├── myfile1
├── myfile2
└── myfile3

3 directories, 7 files

As you can see, there are 3 sub-directories namely dir1, dir2 and dir3 and there are 7 files namely myfile1, myfile2, myfile3, file1, file1.1, file2, and file3 under a root-directory called MyFiles.

Given the provided directory structure under MyFiles/, let's explore how the maxdepth and mindepth options of the find command can be utilized to target our search for files or directories within specific depths.

Using maxdepth

The maxdepth option limits the search to a specified number of levels below the command's starting point.

Example 1:

List everything within MyFiles/ up to 1 level deep (including files and directories directly under MyFiles/, but not further down).

$ find MyFiles/ -maxdepth 1

This would list Dir1, myfile1, myfile2, and myfile3, but it would not list Dir2, file1, file1.1, or anything below Dir2.

Example 2:

List everything up to 2 levels deep.

$ find MyFiles/ -maxdepth 2

This would include Dir1, myfile1, myfile2, myfile3, and also Dir2, file1, and file1.1, but not Dir3 or file2 and file3.

Using mindepth

The mindepth option specifies the minimum depth before find starts returning results.

Example 3:

List everything in MyFiles/ starting from 2 levels deep.

$ find MyFiles/ -mindepth 2

This command skips the direct children of MyFiles/ (such as Dir1, myfile1, myfile2, myfile3) and lists everything from Dir2, file1, file1.1, and below.

Example 4:

Find files starting from 3 levels deep.

$ find MyFiles/ -mindepth 3 -type f

This would list file2 and file3, skipping the files and directories that are less than 3 levels deep.

Combining maxdepth and mindepth

You can combine these options to finely tune your search depth.

Example 5:

List files that are exactly 3 levels deep.

$ find MyFiles/ -mindepth 3 -maxdepth 3 -type f

This command targets files that are 3 levels deep exactly, such as file2 under Dir2. Note that file3 is actually 4 levels deep and wouldn't be included.

Searching for Specific File or Directory

In our previous examples, we explored how to search for all files or directories. In this section, we focus on how to search an individual file or a folder.

Find the "file1" file under the root directory and one level down (i.e., root and one sub-directory):

$ find /path/to/directory/ -maxdepth 2 -name file1

Replace /path/to/directory/ with your own directory path.

Find the "file2" file under the root directory and two levels down (i.e., root, sub-directory level 2, and sub-directory level 3):

$ find /path/to/directory/ -maxdepth 3 -name file2

Find files named "file" between sub-directory level 2 and 4 (i.e. starting from sub-directory level 3 up-to sub-directory level 5):

$ find /path/to/directory/ -mindepth 3 -maxdepth 5 -name file

Find all files named "file3" under all sub-directories starting from the root directory:

$ find /path/to/directory/ -name file3

Feel free to adapt these commands to your use case, and explore other options provided by the versatile find command!

Understanding mindepth and maxdepth Options with a Simple Analogy

If you're a newbie, you may find the use of the maxdepth and mindepth options a little confusing. Here's a simple way to remember these options:

Think of Depth as Distance:

  • mindepth: This is like setting a minimum distance before you start counting or searching. It's like saying, "Start counting or searching only after you've gone at least this deep."
  • maxdepth: This is like setting a maximum distance you're willing to go. It's like saying, "Stop counting or searching once you've gone this deep."

Using an Analogy:

Imagine you're exploring a forest with multiple layers of trails and paths. Each layer represents a different depth level:

  • mindepth: Before you start counting or exploring, you decide on the minimum depth you're willing to start from. For example, you might say, "I'll only count or explore anything after I've gone at least 2 layers deep into the forest."
  • maxdepth: You also decide on the maximum depth you're willing to go. For example, you might say, "I won't count or explore anything beyond 3 layers deep into the forest."

Putting it Together:

  • mindepth 2: This means you start counting or searching from the point where you're at least 2 layers deep into the forest.
  • maxdepth 3: This means you stop counting or searching once you've reached 3 layers deep into the forest.

Visual Representation:

Forest (Root)
├── Layer 1
│   ├── Layer 2
│   │   ├── Layer 3
│   │   │   ├── Layer 4
│   │   │   └── Layer 4
│   │   └── Layer 3
│   ├── Layer 2
│   └── Layer 2
└── Layer 1

In this forest analogy, you can adjust your exploration by setting mindepth to determine where to start and maxdepth to determine how deep you're willing to go.

I hope this analogy helps make it clearer!

Now, let's apply this to our directory example:

If you want to find files starting from the root directory and going down only two levels, use:

$ find /path/to/directory/ -maxdepth 2 -name <filename>

If you want to find files starting from level 3 and going down to level 5, use:

$ find /path/to/directory/ -mindepth 3 -maxdepth 5 -name <filename>

Remember, maxdepth limits how deep you go, and mindepth sets your starting point.

Happy directory exploration!

Conclusion

In this tutorial, we learned how to search for files and directories within a specified range using the mindepth and maxdepth options with the Linux find command, along with examples.

Through these examples, you can see how maxdepth and mindepth options allow for precise control over the depth of your searches within a filesystem, enabling you to target specific areas of your directory structure efficiently.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More