1

I'm searching within Java files for some occurrence of a phrase:

find . -name '*.java' | xargs grep -l 'string'

How do I change this command to print to the shell all of the lines which contain a match?

2
find . -name '*.java' | xargs grep 'string'

find . -name '*.java' | xargs grep -H 'string' # if you want filenames too
| improve this answer | |
  • Or to handle spaces in filenames: find . -name '*.java' -print0 | xargs -0 grep 'string' – Gene Gotimer Jun 29 '10 at 15:22
1
grep -HR "string" *.java
| improve this answer | |
0

Try using ack, available at betterthangrep.com

ack string --java
| improve this answer | |
0

If your filenames contain space, ' or " you should look at GNU Parallel www.gnu.org/software/parallel/

find . -name '*.java' | parallel grep 'string'

Depending on your system this may run faster than the sequential grep as this can use multiple CPUs. Often, though, the speed of the disk will be the limiting factor.

Watch the intro video to GNU Parallel: http://www.youtube.com/watch?v=OpaiGYxkSuQ

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.