mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
Last Updated :
19 Jul, 2024
How to limit search a specified directory in Linux?
There is a command in Linux to search for files in a directory hierarchy known as
‘find’
. It searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left-hand side is false for and operations, true for or), at which point find moves on to the next file name. If no starting-point is specified, `.’ is assumed. The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However the depth of directory traversal can be specified(which are
mindepth
and
maxdepth
).
What are mindepth and maxdepth levels?
- maxdepth levels : Descend at most levels (a non-negative integer) levels of directories below the starting-points. -maxdepth 0 means only apply the tests and actions to the starting-points themselves.
- 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 starting-points.
Given below some examples to illustrate how depth of the directory traversal can be specified using
mindepth
and
maxdepth
- Find the passwd file under all sub-directories starting from the root directory.
find / -name passwd

- Find the passwd file under root and one level down. (i.e root — level 1, and one sub-directory — level 2)
find / -maxdepth 2 -name passwd

- Find the passwd file under root and two levels down. (i.e root — level 1, and two sub-directories — level 2 and 3 )
find / -maxdepth 3 -name passwd

- Find the password file between sub-directory level 2 and 4.
find / -mindepth 3 -maxdepth 5 -name passwd

There are two other ways to limit search a directory in linux :
grep Grep searches the named input FILEs (or standard input if no files are named, or the file name – is given) for lines containing a match to the given PATTERN.By default, grep prints the matching lines.
Examples of grep :
You can search the current directory with grep as follows:

To check whether a directory exists or not

Find the directory under root directory.

Find the directory under root and one levels down.



ack Ack is designed as a replacement for 99% of the uses of grep. Ack searches the named input FILEs (or standard input if no files are named, or the file name – is given) for lines containing a match to the given PATTERN. By default, ack prints the matching lines. Ack can also list files that would be searched, without actually searching them, to let you take advantage of ack’s file-type filtering capabilities. Ack does not have a max-depth option
Examples of ack :
To check a particular directory under the root




mindepth and maxdepth in Linux find() command for limiting search to a specific directory. – FAQs
What do mindepth and maxdepth do in the find command?
In the find command in Linux, mindepth and maxdepth are options used to limit the depth of directory traversal:
- mindepth: Controls how deep
find starts to report results. If you specify mindepth as 2, find will ignore everything at depth 1 (the top-level directory) and start reporting from depth 2.
- maxdepth: Limits how far down in the directory structure
find will go to look for matches. If you specify maxdepth as 1, find will only look at the direct children of the directories specified and no further.
How to use mindepth to limit search results in Linux?
To use mindepth in the find command, specify it followed by the minimum depth you want find to start reporting results. For example, to find all .txt files starting from subdirectories two levels down from the current directory:
find . -mindepth 2 -name "*.txt"
This command will not report .txt files in the current directory or its immediate subdirectories but will start from subdirectories two levels deep.
How to use maxdepth to limit search results in Linux?
To use maxdepth in the find command, specify it followed by the maximum depth you want find to search. For example, to find all .txt files in the current directory and its immediate subdirectories:
find . -mindepth 2 -name "*.txt"
This command will look for .txt files in the current directory (depth 1) and its immediate subdirectories (depth 2).
Can mindepth and maxdepth be used together in find?
Yes, mindepth and maxdepth can be used together in a single find command to finely control the search depth. For example, to find files only in subdirectories exactly two levels deep, you can set both mindepth and maxdepth to 2:
find . -mindepth 2 -maxdepth 2 -name "*.txt"
This command will only find .txt files that are located exactly two directories beneath the current directory.
What are some examples of using mindepth and maxdepth in real scenarios?
Here are a couple of practical examples where mindepth and maxdepth can be particularly useful:
1. Backup Scripts: When creating backups, you might want to exclude the top directory or include only specific subdirectory levels. For instance, backing up only the second-level directories of a project:
find /project -mindepth 2 -maxdepth 2 -type d -exec tar -cvzf {}.tar.gz {} \;
2. System Administration: Searching for specific types of files within a controlled depth range for performance reasons, like finding all log files within the first three levels of /var/log:
find /var/log -maxdepth 3 -name "*.log"
3. Cleaning Up Temporary Files: Removing temporary files that are not within the top-level temporary directories, ensuring that directory structure is preserved:
find /tmp -mindepth 2 -name "*.tmp" -delete