How can I recursively grep particular files in a directory?

14

4

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?

Eric Wilson

Posted 2009-08-06T14:44:25.320

Reputation: 6 388

2

Dupe: http://superuser.com/questions/3512/

– innaM – 2009-08-06T14:52:14.603

1Sorry, I'll look harder next time. Remarkable that the questions were so similar. – Eric Wilson – 2009-08-06T14:59:44.587

1The 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 – 2009-08-06T16:10:42.670

1@richardhoskins, fixed. – John T – 2009-08-06T16:52:14.367

Answers

16

find . -name '*.java' | xargs grep <your pattern here>

arathorn

Posted 2009-08-06T14:44:25.320

Reputation: 8 559

5Better 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 – 2009-08-06T14:55:39.963

1@CoverosGene: You can use the --replace option to xargs to handle that -- like this: ... | xargs --replace grep "{}" <pattern> – arathorn – 2009-08-06T14:59:33.433

thanks much, I knew there must be a command that does what xargs does. – Eric Wilson – 2009-08-06T15:00:30.857

6

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.

user4126

Posted 2009-08-06T14:44:25.320

Reputation: 546

1Meh. 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 – 2009-08-07T11:27:55.093

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 – 2009-08-07T11:54:26.637

Ack has pretty much replaced grep for me. – Ryan C. Thompson – 2009-09-08T23:38:14.140

3

What about:

grep -irn --include="*\.java" somePhrase *

Manatok

Posted 2009-08-06T14:44:25.320

Reputation: 131

0

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

wudeng

Posted 2009-08-06T14:44:25.320

Reputation: 111

3Welcome 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 – 2013-04-09T11:55:41.110