exclude directories from linux find command

How to Exclude Directories in Linux Find

Linux’s find command in Linux is an extremely useful command for searching for files, directories, and sub-directories.

In this guide, I’ll show you how you can exclude certain directories when using the Linux find command so that you can easily find what you’re looking for.

For the purposes of this guide, I’ll be using the Ubuntu 22.04 LTS version. However, the methods I describe will work on all Linux distributions.

Let’s get started!

How to Exclude Directories in Linux Find: 3 Methods

I’ll show you multiple methods and explain each one. You can choose the one that best fits you.

Method 1: Exclude Directories in Find Command By Using the prune Option (Easiest)

prune is a useful option you can add to the find command to prevent the command from printing certain directories. 

This option tells the find command not to go into a specific directory. 

Let’s proceed with the steps on how to use the prune option to exclude directories in the find command.

Option 1: Exclude a Single Directory (Command Run From Anywhere)

In this first case, you can run the command from any directory of your choice. Let’s see how to do that.

  1. For demonstration purposes, I’ve created a test directory in my home directory. In that test directory, there are 5 other non-empty directories, as shown in the screenshot below:
How to Exclude Directories in Linux Find Command
  1. First, add the directory you want to search, find directory/to/search. This will be /home/zunaid/test in my case. 
  2. Now let’s say I want to exclude dir3 from my search. So I will write the -path option followed by the directory you want to exclude from your search like this -path path/to/the/directory/to/exclude. In my case, this will be /home/zunaid/test/dir3, which has the dir3 I wish to exclude. 
  3. Now, you add the -prune option. You can also add other options like -o and -print or more after the -prune option. The final command resulting from Steps 2 to 4 should result in the following: 
find /home/zunaid/test -path /home/zunaid/test/dir3 -prune -o -print
How to Exclude Directories in Linux Find Command

As you can see, everything except dir3 and its content has been printed on the terminal. 

Option 2: Exclude a Single Directory (Command Run From the Search Directory)

Now, what if you don’t want to use this command from the home directory? Instead, you want to use it from the directory where you want to run the search? 

The following steps will show you how.

  1. To do that, simply change the first directory path to a ‘.’ (period) instead of mentioning the absolute path. For example, replace the directory/to/search from find directory/to/search to find. To use the earlier example I’ve shown, this will be:
find . -path /home/zunaid/test/dir3 -prune -o -print
How to Exclude Directories in Linux Find Command

Take note once again, in this example, I first changed my directory and went into the test directory.

To run the command in the current directory, I used a ‘.’ (period) instead of mentioning the absolute path.  

Option 3: Exclude Multiple Directories

You can also exclude multiple directories using the prune option. 

  1. To do that, you must combine each path to exclude using the -o (Logical OR) operator. 
  2. Next, use backslash and parentheses \() to hold the whole expression like this \( -path path/to/the/directory1/to/exclude -prune -o -path path/to/the/directory2/to/exclude -prune \). Have a look at my example command below:
find . \( -path ./dir4 -prune -o -path ./dir5 -prune \) -o -print
How to Exclude Directories in Linux Find Command

This command, as shown in the screenshot above, has excluded dir4 and dir5 directories and only displays the other ones, as expected.

Method 2: Exclude Directories in Find Command By Using the ! operator

If you’re familiar with programming languages, you might know that the ! exclamation mark symbol implies a logical NOT.

You can use this operator with the find command to exclude targeted directories. 

Let me show you how.

Option 1: Exclude a Single Directory

Now let’s see how this command can exclude a directory. 

  1. Going back to the previous example, let’s exclude dir1 from the search this time. The first option you must add is the search directory path directory/to/search. For demonstration purposes, I will use the ‘.’ period symbol to use it from the directory where I want to run the search. 
  2. Next, add -type f so that the find command only searches for regular files. 
  3. Now we will add the ! (exclamation mark) operator, which will negate the matching expression that comes after it, that is, the path to exclude the specific directory. You need to add -path and then wrap the path in both asterisks (*) and single quotes (‘’) like ‘*path/to/the/directory/to/exclude*’. The final command is as follows based on removing dir1 as an example:
find . -type f ! -path '*/dir1/*'
How to Exclude Directories in Linux Find Command

As you can notice in the screenshot above, the command returns all files within the sub-directories except the one we wanted to exclude.

Option 2: Exclude Multiple Directories

Now, let’s see how to exclude multiple directories using the ! (exclamation mark) operator.

  1. For multiple directories, you can chain the directories using the -o operator and wrap the whole expression in a backslash and parentheses \(), as shown below. Take note to include the -path for each directory you add::
find . -type f ! \( -path '*/dir1/*' -o -path ‘*dir2/*’ )
How to Exclude Directories in Linux Find Command

The result excludes dir1 and dir2, as expected. 

Similar to the ! operator, there’s also a not operator. Let’s see how you can use that in the next method.

Method 3: Exclude Directories in Find Command By Using the not operator

The not operator is similar to the ! operator, except that the not operator is not POSIX compliant. 

The syntax to use this command is the same too, just that we’ve replaced the ! with NOT. The rest is the same. Now let’s use this command to exclude some directories.

Option 1: Exclude a Single Directory

  1. In my example, I want to exclude dir1. The first option to add is the search directory path directory/to/search. For demonstration purposes, I will use the ‘.’ period symbol to use it from the directory where I want to run the search. 
  2. Next, add -type f so that the find command only searches for regular files.
  3. After that, add the -not command, which will negate the matching expression that comes after it, that is, the path to exclude the specific directory. You need to add -path and then wrap the path in both asterisks (*) and single quotes (‘’) like ‘*path/to/the/directory/to/exclude*’. The final command is as follows based on removing dir1 as an example:
find . -type f -not -path '*/dir1/*'
How to Exclude Directories in Linux Find Command

Notice in the above screenshot that dir1 is excluded from the result. 

Option 2: Exclude Multiple Directories

  1. You can also exclude multiple directories using the not operator. So if I want to exclude both dir1 and dir2, this is the command I’ll use:
find . -type f -not \( -path '*/dir1/*' -o -path ‘*dir2/*’ )
How to Exclude Directories in Linux Find Command

And that successfully excluded dir1 and dir2 from our search.

Conclusion

By now, you should know how to exclude directories in the Linux find command.

With this knowledge, you can speed up your search process.

Want to learn more about directory management in Linux? Check out the other tutorials, like how to delete a directory with files in Linux or how to go up a directory.

If this guide helped you, please share it.

Related Posts