Find Command Exclude Directories From Search Pattern

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 16 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
16 comments… add one
  • kashyap Jan 25, 2011 @ 9:47

    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

  • narcis May 4, 2011 @ 6:43

    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

  • sandeep May 10, 2011 @ 10:07

    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

  • sreedhar May 31, 2011 @ 19:17

    we have the below directory structure:


    I want to exclude the below 2 directories from my search but show up the other 2

    How do I do that using the above stated commands.

    Thanks for the help

  • Joe Oct 31, 2011 @ 19:30

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

  • Chris Jan 5, 2012 @ 1:36

    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

  • Simas Aug 1, 2012 @ 8:52

    Chris, thanks a lot, due to your post I was able to search for files containing “somephrase” in directory and its subdirs excluding some speciffic subdirectories with this command:
    find /path/to/dir/ -exec grep -q somephrase {} \; -print -o -type d -wholename “/path/to/dir/speciffic/dir/*” -prune

    hopefully somebody will find it usefull

  • Thomas A Andersion Oct 20, 2012 @ 19:43

    find . \( -name results -prune \) -o \( -name typ_testout -prune \) -o \( -name obj-testgen -prune \) -o \( -name obj-sim -prune \) -o -type f -exec grep -w abf {} \; -print

    I was trying to find the list of files having the string “abf”. But I wanted to exclude search results in the “results”, “typ_testout”, “obj-testgen”,”obj-sim” directories. So I used the above command and it worked perfectly fine for me. The secret is the “-o” option after each expression. So the find command matches against multiple expressions.

  • Thomas Dec 7, 2012 @ 13:07

    Thanks for the article, using it and the man page, here is a version using wholename to ‘prune’ absolute paths:

    find / \( -wholename '/home/*' -prune \) -o \( -wholename '/other/folder/*' -prune \) -o -name \*.bak -print


  • lxdorney Feb 12, 2013 @ 8:37

    find all directories except tmp directory:
    find /path/to/dest -maxdepth 2 -type d \( ! -name tmp \) -print
    how about show only the directory with no subtmp?

  • Pr1y3nd Mar 18, 2013 @ 19:12

    find . -name “*.mp3” -and -not -path “*Trash*”
    will find all mp3 files and exclude any folder containing the letters “Trash”

    • roan Jul 10, 2013 @ 14:35

      +++ thank you, been looking to exclude my .svn folder when using find — this did the trick: find . -type f -and -not -path “*.svn*” -print

    • Sahil Aug 22, 2013 @ 16:02


  • BASTA! Jun 20, 2014 @ 11:16

    The real general problem is that of excluding certain directories by name, *AND ALSO AVOIDING TRAVERSING THEM* because they may contain *HUGE* subtrees that would take ages to scan, which is *PRECISELY* why we want to exclude them.

    Additionally, the directories to be excluded may be located deeper than immediately below the start directory, i.e. if the intention is to exclude directories namet _thumb, then *ALL* of the following should be excluded:


    The answers above either don’t work at all, fail to avoid traversing the excluded directories, or would only exclude the first line.

    Is the solution satisfying *ALL* the above requirements even possible with find?

  • sugatang itlog Jul 16, 2014 @ 11:13

    Due to some bugs and versions of find, some works and not.

    Though a simle find with grep will do.

    Find the files and directories with ganglia on it, except for the directories with name Downloads.
    sudo find / -iname “*ganglia*” | egrep -vi “downloads”

    Or you can exclude specific directory like, find files or directories with ganglia except in the directory /home/simpleboy/Downloads
    sudo find / -iname “*ganglia*” | egrep -v “\/home\/simpleboy\/Downloads”

    Thanks More power nixCraft!

  • Litton Oct 2, 2015 @ 21:28

    In one go:

    find / \( -path /exclude-folder1 -o -path /exclude-folder2 \) -prune -or -iname ‘*look-up*’ -exec ls -ld {} \;

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum