Find Command Exclude Directories From Search Pattern

September 8, 2019

Rate this post

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.