Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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:

share|improve this answer

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

Your Answer


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.