Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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/

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.