3

I'm using find command along with -exec something argument. Can I exclude some directories/name that match regex (something like rsync's --exclude)? Will -prune be enough if I want to exclude many things?

2 Answers 2

3

You can exclude certain directories while using the find command under Ubuntu.

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

Taken from:

http://www.cyberciti.biz/faq/linux-unix-osx-bsd-find-command-exclude-directories/

1

For a working solution (tested on Ubuntu 12.04).

find ! -path "*dir1*" -iname "*.mp3"

will search for mp3 files in the current folder and subfolders except in dir1 subfolder.

find ! -path "*dir1*" ! -path "*dir2*" -iname "*.mp3"

to exclude dir1 AND dir2.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .