Exclude a directory or multiple directories while using find command

As a Linux server administrator or DevOps engineer we need to use find command quite frequently to find a lot of stuff from the server. We can use the find command to find files and directories based on different things; like based on file permission, ownership, size, access time etc. In this blog article we are discussing how we can exclude some directories while doing the find command.

If you have a lot directories, it takes time to do the find operation. This exclude will help you to reduce the execution time while doing the find command.

Is it possible to exclude a directory with find command? Exclude directories while doing running find command? I have a lot directories and how to exclude selected directories while performing the find command to save the find execution time.

Yep, the command FIND has wide range of options to search what you actually looking for. I have already listed different switches and its usages with examples. Here we go for excluding some directories from our find job.

In some cases, we have to exclude some directories from our search pattern to improve the search speed or efficiency. If the server has a lot of directories and we are sure about that the file / directory that we are searching is not in some directories, we can directly exclude those to improve the performance. The result will be faster as compared to the full search.

Quick view on “Find command and switches

There are different ways to exclude a directory or multiple directories in FIND command. Here I’m listing some methods!

To explain this, I created the following directories and files:

  •  “cry“, “bit” and “com” directories.
  • ” findme “: The test file in all directories.

Lets see the output:

# find -iname findme

Method 1 : Using the option “-prune -o”

We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command.

See the example:

# find ./ -path ./bit/* -prune -o -iname findme -print

The directory “bit” will be excluded from the find search!

Method 2 : Using “! -path”

This is not much complicated compared to first method. See the example pasted below:

# find -iname findme ! -path ./bit/*

If you are interested to read some Kubernetes topics you can check this page https://www.crybit.com/category/devops/k8s/

Method 3 : Simple 🙂

Yes, it’s very simple. We can ignore the location by using inverse grep “grep -v” option.

This is not the recommended way. It do the grep after performing the find operation. So there is no advantage considering the find command execution time. It does the find first then exclude the specific string.

See the example:

# find -iname findme|grep -v bit

Excluding multiples directories

Similar way we can exclude multiple directories also. See the sample outputs:

# find -iname findme ! -path ./bit/* ! -path ./cry?*


# find -iname findme | egrep -v "bit|cry"

That’s it. Compose it your own ways!!


1, 10+ commonly using find command switches with example Unix/Linux
2, How to find the total number of connections for a cPanel account from the log files
3, How to check all opened files in a directory – lsof command

Post navigation

Arunlal Ashok

DevOps Engineer. Linux lover. Traveller.
Always happy for an open discussion! Write to arun ((@)) crybit ((dot)) com.

2 thoughts on “Exclude a directory or multiple directories while using find command

  1. Fantastic tutorial. Well constructed with the “findme” files in each directory, to verify what has been found and what excluded.

    I’ve kept this in my set of Linux tutorial pages.

Leave a Reply

Your email address will not be published.