Find Command Exclude Directories From Search Pattern

by Vivek Gite on July 17, 2010 · 6 comments

How do I exclude certain directories while using the find command under UNIX or Linux operating systems?

You can use the find command as follows to find all directories except tmp directory:

find /path/to/dest -type d \( ! -name tmp \) -print

Find all directories except tmp and cache:

find /path/to/dest -type d \( ! -name tmp \) -o \( ! -name cache \) -print

The -prune option make sure that you do not descend into directory:

find /path/to/dest -type d \( ! -name tmp \) -o \( ! -name cache -prune \) -print

You can find all *.pl find except in tmp and root directory, enter:

find /  \( ! -name tmp \) -o \( ! -name root -prune \)  -name "*.pl" -print

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 6 comments… read them below or add one }

1 kashyap January 25, 2011

find / \( ! -name tmp \) -o \( ! -name root -prune \) -name “*.pl” -print

it’s also displaying the files in tmp directory ?? this isn’t working
using findutils.x86_64 1:4.2.27-6.el5 version

Reply

2 narcis May 4, 2011

On my Ubuntu 11 this command will search for all “pl” files in the entire / dir but will not descend in /tmp:

find . -path ./tmp -prune -o -iname “**.pl” -print

Reply

3 sandeep May 10, 2011

To exclude multiple directories, the option to use is -a, which is AND operation; not -o as listed in the above article.

Therefore, to find all directories except tmp and cache

find /path/to/dest -type d \( ! -name tmp \) -a \( ! -name cache \) -print

Reply

4 sreedhar May 31, 2011

we have the below directory structure:

/secure/data/bus/PREP_MDATA/preserve
/secure/data/bus/PREP_TDATA/preserve
/secure/data/bus/PREP_QDATA/preserve
/secure/data/bus/PREP_RDATA/preserve

I want to exclude the below 2 directories from my search but show up the other 2
/secure/data/bus/PREP_TDATA/preserve
/secure/data/bus/PREP_QDATA/preserve

How do I do that using the above stated commands.

Thanks for the help

Reply

5 Joe October 31, 2011

I’m just trying to exclude ONE directory, and the code doesn’t work. All directories are printed.

Reply

6 Chris January 5, 2012

Joe,
Try this
This will list a non case sensitive list of all files with some phrase but will exlude some other folder with some non case sensitive phrase in it.
Remember if you just want it to list all files you can use * or *.ext for all files with .ext etc.
It seems counter intuitive to use the -o flag but it works here on csh

The dot right after the find command signifies THIS directory or whatever directory you are in. You can always replace that with another directory path.

find . -type f -iname “insert_file_names_you_want_listed_here” -print -o -type d -iname “insert_folder_name_you_dont_want_here” -prune

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 8 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: