mindepth and maxdepth in Linux find() command for limiting search to a specific directory. Last Updated : 19 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 maxdepthFind the passwd file under all sub-directories starting from the root directory. find / -name passwdFind the passwd file under root and one level down. (i.e root — level 1, and one sub-directory — level 2) find / -maxdepth 2 -name passwdFind 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 passwdFind the password file between sub-directory level 2 and 4. find / -mindepth 3 -maxdepth 5 -name passwdThere 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 notFind 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 optionExamples of ack :To check a particular directory under the root Comment More infoAdvertise with us Next Article mindepth and maxdepth in Linux find() command for limiting search to a specific directory. K Kishlay Verma Improve Article Tags : Operating Systems Linux-Unix linux-command 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 How to use 'cp' command to exclude a specific directory The cp command is used to create an exact copy of the contents of files and directories but with a different name and location. It works using the terminal.Syntaxcp [source_file/directory] [destination]Where cp is used for the copy, source_file provides the exact file or directory or folder from whe 4 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 Display Path of an Executable File in Linux | Which Command In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In th 6 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 How to Find Files Modified in Last N Number of Days in Linux? Sometimes, we want to find the files which we created or modified in the last N number of days. Sorting the files on the basis of date helps in this case, but that's the traditional way of doing the task and is not efficient. This article is all about searching such files using the find command. Met 3 min read How to Recursively Find all Files in Current and Subfolders Based on Wildcard Matching in Linux Traversing through a directory tree to locate files is a common operation performed by most filesystem management software. This utility is in the form of command-line commands in most Operating Systems. In this article, you will learn how to find files using Wildcard Matching in Linux Operating Sys 5 min read How to Check the Size of a Directory in Linux Many times during working on Linux we may encounter situations, where we want to check the total size occupied by a directory, especially when working with large files and directories. Knowing the file or directory size can help a user perform tasks like storage allocation, size comparison, etc. At 7 min read Like