How to Stop the Find Command from Searching Subdirectories in Ubuntu

The Infinite Crawl

By default, the Linux find command is incredibly aggressive. If you tell it to search your home directory, it doesn’t just look at the files sitting on your desktop; it instantly burrows down into every single sub-folder, hidden directory, and nested cache file it can find. If you are searching the root directory (/), the terminal will attempt to parse millions of files, causing the search to take several minutes and flooding your screen with irrelevant results. If you know exactly which folder a file is in and you just want to filter the contents of that specific folder without digging any deeper, you must physically restrain the search engine.

Using the -maxdepth Flag

You can force the find command to stay on the surface by utilizing the -maxdepth flag.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To search the /var/www/ directory for a file named “index.html” without searching inside any of its subfolders, type the following command exactly:
  3. sudo find /var/www/ -maxdepth 1 -name "index.html"
  4. Press Enter.

Controlling the Depth

The number 1 is the critical component. It tells the kernel to look at the current directory (level 1) and then immediately stop. If you want to allow the search to peek exactly one folder deep (for example, to check the folders immediately inside the web directory, but no deeper), you simply change the number:

sudo find /var/www/ -maxdepth 2 -name "index.html"

This ensures your search completes in milliseconds rather than minutes, completely eliminating the aggressive recursive crawl.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.