Skip to main content
Quick Tip

Exclude Directories While Using Find Command

By default, find command searches in the specified directory and all its subdirectories. You can refine your search by excluding directories while using find command.

Sagar Sharma

Warp Terminal

Find is an extremely powerful command for searching for anything you want on your Linux system.

By default, it searches in the specified directory and all its subdirectories.

You may not always want that. You can refine your search by excluding directories from the find command search.

In this tutorial, I'll show various ways to exclude directories while using the find command.

Method 1: Using the prune option

First, let me bring light to how you're about to use the find command with the prune option:

find [path] -path [directory to exclude] -prune -o -print

For example, I've made a directory named prune which contains the following files and directories:

use tree command to map files and directories

So how about excluding music directory while performing a search?

find /home/sagar/prune -path /home/sagar/prune/music -prune -o -print
exclude directory using prune option in find command

As you can clearly see, it only went through the text and images directory as I excluded the 3rd directory.

Exclude multiple directories

Quite easy right? But what about when you want to exclude multiple directories? That can easily be done using the -o operator.

For example, I'll be excluding music and text directory:

find . \( -path ./music -prune -o -path ./text -prune \) -o -print
exclude multiple directories with find command
Exclude multiple directories from the find search

In simple terms, you have to chain your directories with -prune and -o inside () as shown fashion to exclude multiple directories.

But find is not bound to only search for files but when paired with exec, can execute scipts, programs, or even commands over output:

Find Exec Command in Linux: 9 Useful Examples
Find works on searching files based on a number of criteria. The exec command gives you the ability to work on those results. Here are some examples of find exec command combination.

Method 2: Using the not operator

Using the not operator is easy compared to what I explained above as syntax is quite simple to grasp:

find [path] -type f -not -path '*/directory to exclude/*'

For example, let's exclude the music directory using the not operator:

find . -type f -not -path '*/music/*'
exclude directory using not option in find command

As you can clearly see, the search results do not include any files related to the music directory.

Method 3: Using the ! operator

Yet another easy way to exclude directories while searching is to use the ! operator.

The syntax is similar to what I explained above but a little short in length:

find /path/ -type f ! -path '*/directory to exclude/*'

Let's say I want to exclude a directory named text so my command would be:

find . -type f ! -path '*/text/*'
exclude directory using ! operator in find command

But that's not it. Being one of the most extensive commands, find can also search for files based on modification time.

Bonus tip: Exclude Subdirectories in find command

Well, this is a bit different as this section is going to utilize the term called search depth.

This means I will be specifying how deeper the find utility will search. Here, deep means the layers of the file system. The general structure of the file system looks like this:

linux filesystem

And you can adjust the depth of search by using the maxdepth and mindepth options.

So when you use the maxdepth, you're giving limits to the find utility, or in simple terms, you're saying "don't go any further than this limit".

While mindepth is all about where to start the search and will look for every available file at every layer.

So let's suppose I want to search for text files that are available at the first layer so I'll be using maxdepth to control the search:

find . -maxdepth 1 -type f -name  "*.txt"
use maxdepth with find to exclude dirctories while searching

But what if you want to search into a specific layer that sits in the middle? Well, that's where the mindepth comes to play!

Let's suppose I want to search for PNG images specifically inside the Images directory (2nd layer) so I will use maxdepth and mindepth to 2 forcing find to search for a specific directory.

find . -maxdepth 2 -mindepth 2 -type f -name  "*.png"
use maxdepth and mindepth with find command

Be creative with maxdepth and mindepth and you'll be able to search for specific files in no time!!

Final Words

This was my take on how to exclude directories while searching files by various methods and if you still have any doubts, feel free to ask in the comments.

Sagar Sharma