Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Consider using ack: – Dennis Williamson Apr 3 '10 at 19:01
up vote 2 down vote accepted
find . -name '*.java' | xargs grep 'string'

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

Try using ack, available at

ack string --java
share|improve this answer

If your filenames contain space, ' or " you should look at GNU 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:

share|improve this answer

Your Answer


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.