Open In App

mindepth and maxdepth in Linux find() command for limiting search to a specific directory.

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?



Given below some examples to illustrate how depth of the directory traversal can be specified using mindepth and maxdepth

There are two other ways to limit search a directory in linux :



  1. grep
  2. 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.
    

  3. 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
  4. Examples of ack :

    To check a particular directory under the root
    

    Reference : Linux manual page

Article Tags :