How to Exclude the Current Directory in Searches in Ubuntu (find -mindepth)

The Top-Level Clutter

When running a broad search or modifying files using the find command, the search engine always starts by evaluating the very directory you specified as the root of the search (e.g., /var/www/), before it dives into the subfolders (e.g., /var/www/html/). This becomes highly problematic if you are trying to change the permissions of every sub-folder inside a directory, but you strictly do not want to alter the permissions of the parent directory itself. To prevent the parent folder from being included in the search results, you must explicitly instruct the find command to ignore the top level of the directory tree.

Using the find Command with -mindepth

The -mindepth flag forces the search engine to skip a specified number of levels down the directory tree before it begins evaluating files.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To search for all directories inside /var/www/, but strictly ignore the /var/www/ folder itself, type the following command exactly:
  3. sudo find /var/www/ -mindepth 1 -type d
  4. Press Enter and provide your administrator password.

Targeting Sub-Directories

The -mindepth 1 argument tells the search engine: “Do not start looking for matches until you are at least one level deep.” This completely eliminates the root search path (level 0) from the output. If you only wanted to search inside the sub-sub-directories (e.g., ignoring both /var/www/ and /var/www/html/, but searching inside /var/www/html/images/), you would simply change the flag to -mindepth 2.

Get the best tech tips delivered straight to your inbox.

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