-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.
SourceFile.java
doesn't containtarget
AFAICT.*.java
should be"*.java"
without quotes-name
interprets your argument as literal expression