Skip to content

How to limit directory depth with find command

The ‘find’ command with -maxdepth is a powerful tool in the Linux operating system. It is used to recursively search for files and directories in a given directory and its subdirectories. The -maxdepth flag is used to specify the maximum depth of the search. For example, if the -maxdepth is set to 2, the search will only look at the given directory and its immediate subdirectories. This means that it will not look in any of the subdirectories.

This makes the ‘find’ command with -maxdepth a great way to quickly search for something without having to go through all the subdirectories. For example, I have to search the folder named “node_modules” under all the directories of the current directory only:

find . -maxdepth 2 -type d -name "node_modules" 

To search in the current directory only, set the -maxdepth value to 1.

Overall, the ‘find’ command with -maxdepth is a powerful and versatile tool in the Linux operating system that can save you time when you need to quickly find something or make changes to a group of files or directories.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top