Tuesday, September 14, 2010

Find Command Exclude Directories From Search Pattern

SkyHi @ Tuesday, September 14, 2010
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
 
Your find command may not support -delete option, so use it as follows:
# find /var/pub/ftp/incoming -type f -print0 | xargs -0 -I file rm -f file
OR
# find  /var/pub/ftp/incoming -type f -exec rm -f {} \;
 
You can also select file types. In this example, delete all *.exe files, enter:   
# find  /var/pub/ftp/incoming -type f -iname "*.exe" -exec rm -f {} \;
 
 
 
To find all symlinks to /etc/resolv.conf, use the find command as follows:
# find /path/to/dir -lname /path/to/file
# find / -lname /etc/resolv.conf
 
/path/to/file is a symbolic link whose contents match shell pattern 
pattern.  The metacharacters do not treat / or . specially. The -ilname 
FILE options is like  -lname,  but  the  match  is  case insensitive:

# find / -ilname resolv.conf  
  
 

Find Command Exclude Directories From Search Pattern

 
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
  
REFERENCES
http://www.cyberciti.biz/faq/linux-unix-osx-bsd-find-command-exclude-directories/
http://www.cyberciti.biz/faq/deleting-file-in-many-subdirectories-linux-unix-command/ 
http://www.cyberciti.biz/faq/linux-unix-osx-bsd-find-command-exclude-directories/