How to use -path and -prune with find

OK, I'm trying search and destroy tabs again.

This time I'm having trouble excluding certain directories from my search.

Here is what I have tried and it is not ignoring the top level build directory:

find . -path ./build -prune -name \*.java -o -print | xargs grep -i ' '

I don't understand the -o and -print either. Don't they default to printing them all the time? They seem to be necessary when I was experimenting with using -regex instead of -name.

Thanks,
Siegfried

Hi siegried, find is ignoring the contents of the directory build, but still reporting the empty directory. So the pruning works. It is just that is you pass it as a parameter to grep -i then it will still grep in all the files in that directory ...

You could try:

find !(build) -name \*.java | xargs grep -i ' '

( tab character between quotes lost in translation )

Under /home/brad/runksh I have a file called test.funcs
under /home/brad/runksh/lib I have a file called runksh.funcs
under /home/brad/scratch I have sc.funcs: -

 
/home/brad/runksh ls -l
-rwxr-xr-x 1 brad root 12887 2009-03-08 18:22 lib/runksh.funcs
-rw-r--r-- 1 brad root 0 2009-10-23 22:04 scratch/sc.funcs
-rw-r--r-- 1 brad root 0 2009-10-23 21:51 test.funcs

To exclude lib: -

 
TX5XN:/home/brad/runksh>find . \( -name lib -type d -prune \) -o -name \*.funcs -print 
./test.funcs
./scratch/sc.funcs

You do need the print although I don't know why: -

 
TX5XN:/home/brad/runksh>find . \( -name lib -type d -prune \) -o -name \*.funcs 
./test.funcs
./lib
./scratch/sc.funcs

OK, that helps!

Now (I'm just curious) how do I exclude multiple directories using the !(build) syntax?

This is mostly working except it does not exclude "build.xml"!

find . \( -name build -type d -prune \) -o \( -path ./.metadata -type d -prune  \) -o \( -name apollo\* -type d -prune  \) -o \( -name env -type d -prune  \) -o -name \*.xml -o -name \*.java -o \( -name build.xml -prune \)  |  xargs grep -n "	"

Here I am trying to exclude the top level directory called "build" as well as the multiple files inside ./src named build.xml.

Thanks,
Siegfried

I have never tried the !(build) syntax so would not mind playing around with it.

It would help if you actually posted a cut down ls -l output of the directory structure you are talking about rather than giving a verbal description. That way people can replicate it and try things out.

Just to clarify: the !(build) syntax is not part of the find command but it is a pattern marching operator that tells the shell to expand anything but the build directory and pass that the result on to find.

That's really interesting, Thanks!