Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Trying to exclude a set of directories from a find is driving me nuts! What I want to do is look for all .java files in a directory tree, but ignore all those in test directories. So, I've tried:

find . -name "*.java" -not -path "test"

and

find . -name "*.java" -path "test" -prune

and several variations thereof. However - depending on the variation - I either get all the java files (including those in test directories), or none of them. I looked at other questions here on SU (e.g. this one and this one), but either they don't address my situation, or I'm missing something.

I'm using gnu find 4.2.27 (fwiw, on centos 5.5 w/ gnu bash 3.2.25).

Edit: Sorry, I should have originally specified that I need to do a

-exec grep blah {} \;

so piping through grep -v won't work for me in this situation.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

This should work:

find -path '*/test/*' -prune -o -name '*.java' -exec grep blah {} \;
share|improve this answer
2  
-path '*/test/*' -prune is simpler expressed as -name test -prune. If you replace the \; by a +, the grep command will be executed fewer times, and will (almost always) show file names on matches. If you add -H after grep, the matches will always include file names. –  Gilles Nov 8 '10 at 23:42
    
@Gilles: Thanks for the excellent input; someone local to me suggested -name test -prune, which does work for this case. However, I can imagine that if the search was different using -name test -prune would exclude files named test if there were any. –  GreenMatt Nov 9 '10 at 14:23
1  
@GreenMatt: Yes. Here the issue doesn't arise since test and *.java don't intersect. Otherwise, both cases make sense. If you only want to exclude directories named test, use find -name test -type d -prune -o …. –  Gilles Nov 9 '10 at 18:15

Consolidating the above-mentioned tips, this searches for PHP files recursively but excludes several directories and then zips them up.

find -name locallibs -prune -o -name libs -prune -o -name wiki -prune -o -name tmp -prune -o -name '*.php' -exec zip all.zip {} \;

share|improve this answer

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.