Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

How can I find every file and directory matching a pattern, excluding one directory using find?

Say I have the following file structure;


how would I get the following output using find:


I have tried using both of the following command:

find . -name 'foo-exclude-me' -prune -o -name 'foo*'
find . -name 'foo*' \! -path './foo-exclude-me/*'

but both of them return this:

./foo-exclude-me # << this should be excluded

How can I properly exclude the foo-exclude-me directory?

share|improve this question
Try using foo-exclude-me instead of ./foo-exclude-me. Also try appending -print at the end of the command. – n0pe Mar 7 '13 at 17:55
@MaxMackie perfect :) – Tyilo Mar 7 '13 at 18:00
It fixed it? Which was the fix that worked for you? I can put it in an answer instead of a comment. – n0pe Mar 7 '13 at 18:01
@MaxMackie using both -print and foo-exclude-me instead of ./foo-exclude-me. – Tyilo Mar 7 '13 at 18:04
Awesome, looks like Gilles beat me to it though :) – n0pe Mar 7 '13 at 18:13
up vote 7 down vote accepted
find . -name 'foo-exclude-me' -prune -o -name 'foo*' -print

With no -print, the implicit default action applies to every match, even pruned ones. The explicit -print applies only under the specified conditions, which are -name 'foo*' only in the else branch of -name 'foo-exclude-me'.

Generally speaking, use an explicit -print whenever you're doing something more complex than a conjunction of predicates.

Your second attempt with ! -path './foo-exclude-me/*' didn't work because ./foo-exclude-me doesn't match ./foo-exclude-me/* (no trailing /). Adding ! -path ./foo-exclude-me would work.

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.