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 new to linux and grep, and trying to find my way around.

By using find -name *.java I am able to find the names of all of the java files in a particular directory. Suppose I want to count the number of times foo occurs in these files, how would I do that?

I'be been trying things like:

grep -r "foo" *.java

and getting responses like:

grep:  *.java:  No such file or directory

Any ideas?

share|improve this question
Dupe: – innaM Aug 6 '09 at 14:52
Sorry, I'll look harder next time. Remarkable that the questions were so similar. – Eric Wilson Aug 6 '09 at 14:59
The title of 3512 could be a lot better. "Basic grep usage question" doesn't tell you a whole lot about the actual question. We'll probably have more dupes unless that is fixed. – Richard Hoskins Aug 6 '09 at 16:10
@richardhoskins, fixed. – John T Aug 6 '09 at 16:52
up vote 15 down vote accepted
find . -name '*.java' | xargs grep <your pattern here>
share|improve this answer
Better yet, use null terminated items so that a file name with a space in it doesn't get split into two file names: find . -name '*.java' -type f -print0 | xargs -0 grep <your pattern here> – CoverosGene Aug 6 '09 at 14:55
@CoverosGene: You can use the --replace option to xargs to handle that -- like this: ... | xargs --replace grep "{}" <pattern> – arathorn Aug 6 '09 at 14:59
thanks much, I knew there must be a command that does what xargs does. – Eric Wilson Aug 6 '09 at 15:00

There is a tool specially designed for this type of need: ack.

ack is a tool like grep, aimed at programmers with large trees of heterogeneous source code

Also read the "Top 10 reasons to use ack instead of grep." at the ack page.

share|improve this answer
Meh. A real unix tool needs a recursive name that referring its heritage... like abtg [ABTG's Better Than Grep], or something. ACK? The only good think about that is that the author of the improved NACK already have a name picked out ;) – Mikeage Aug 7 '09 at 11:27
One of the reasons I really love ack is that you can specify file types. For example, I with "ack --html <pattern>" it searches all my .html, .htm, .xhtml, .dhtml, etc. files in one go. – Mark van Lent Aug 7 '09 at 11:54
Ack has pretty much replaced grep for me. – Ryan Thompson Sep 8 '09 at 23:38

What about:

grep -irn --include="*\.java" somePhrase *
share|improve this answer

find . -type f -name '*.java' -print0 | xargs -0 grep -wo 'foo' | wc -l

share|improve this answer
Welcome to SuperUser! You'll find that your answers get more attention when that are to more current questions (this one is from 2009) and when you explain why your answer is better than the already accepted one. – Eric Wilson Apr 9 '13 at 11:55
@EricWilson thanks for the advise :) – wudeng Apr 10 '13 at 1:18

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.