I am trying to use find/grep to find some text patterns in my code base.  I want to exclude non-source directories into which my build process duplicates source files:find . -name *.java -not -name *target* -exec grep -l 'someText' {} \; 2>/dev/nullUnfortunately, I still get target directories in the result, e.g.:./toplevel/src/com/domain/toplevel/module/SourceFile.javaWhat am I doing wrong and how do I fix it?
-not -name does not work with find
find
-name pattern matches on the file name. SourceFile.java doesn't match *pattern*, so it is included. If you want not to descend into directories whose name contains target, then you'd need:find . -name '*target*' -prune -o -name '*.java' \  -exec grep -sl 'someText' {} +(remember to quote patterns, otherwise they can be expanded by the shell).For several patterns:find . \( -name 'pattern1' -o -name 'pattern2' \) -prune \  -o -name '*.java' -exec grep -sl 'someText' {} +(AND (-a) is implied when not specified between two predicates. AND has precedence over OR (-o), hence the brackets above).Note that ! is the portable/standard equivalent of -not.If you want to match on the full path, then you need to use -path instead like:find . -name '*.java' ! -path '*target*' -exec grep -sl someText {} +But that's not stopping find from descending into *target* directories, which is a bit of a waste since find won't report any file in there.