Bootstrap FreeKB - Linux Commands - find command - mindepth and maxdepth
Linux Commands - find command - mindepth and maxdepth

Updated:   |  Linux Commands articles

The find command without any options will list the files and directories in and below your present working directory, including hidden files and directories.

~]# find
.
./.bash_logout
./.bash_profile
./.bashrc
./.bash_history
./my-files
./my-files/foo.txt
./my-files/bar.txt

 

To list the files and directories in and below a directory other than your present working directory, you will simply include the directory to search (the /home directory in this example).

~]# find /home
/home/john.doe/
/home/john.doe/.bash_logout
/home/john.doe/.bash_profile
/home/john.doe/.bashrc
/home/john.doe/.bash_history
/home/john.doe/my-files
/home/john.doe/my-files/foo.txt
/home/john.doe/my-files/bar.txt
/home/jane.doe/
/home/jane.doe/.bash_logout
/home/jane.doe/.bash_profile
/home/jane.doe/.bashrc
/home/jane.doe/.bash_history

 

The maxdepth option can be used to specify how many directories the find command should search. Often, -maxdepth 1 is used so that the find command only lists the files and directories in your present working directory, or in a specific directory. In this example, the /home/john.doe/my-files/foo.txt and /home/john.doe/my-files/bar.txt files are not included in the results.

~]# find /home/john.doe -maxdepth 1
/home/john.doe/
/home/john.doe/.bash_logout
/home/john.doe/.bash_profile
/home/john.doe/.bashrc
/home/john.doe/.bash_history
/home/john.doe/my-files

 

The mindepth option can be used to exclude the present working directory or specified directory from the results. In this example, the /home/john.doe directory is excluded from the results.

~]# find /home/john.doe -maxdepth 1
/home/john.doe/.bash_logout
/home/john.doe/.bash_profile
/home/john.doe/.bashrc
/home/john.doe/.bash_history
/home/john.doe/my-files
/home/john.doe/my-files/foo.txt
/home/john.doe/my-files/bar.txt

 

mindepth and maxdepth are often used together.

~]# find /home/john.doe -mindepth 1 -maxdepth 1
/home/john.doe/.bash_logout
/home/john.doe/.bash_profile
/home/john.doe/.bashrc
/home/john.doe/.bash_history
/home/john.doe/my-files

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter bcaa0f in the box below so that we can be sure you are a human.