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




Similar Reads
How to Exclude Certain Paths With the find Command in Linux The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in
3 min read
How to Count Files in Directory Recursively in Linux When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory
5 min read
Difference between locate, which and find Command in Linux In Linux, there are multiple utilities available to locate files efficiently within a system. Three of the most commonly used commands for this purpose are:findlocatewhichEach command serves a similar function but differs in the way they search and the data they return. In this article, we'll explor
5 min read
Linux - Installing locate Command to Find Files In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is a
2 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read