I don't know a great deal about commands.

I am using OS X.

I need to find files in a directory with the .php extension and the string 41 somewhere in the file's contents.

I tried using grep.

grep -R 41 *.php

This however only seemed to find files in the cwd and not in sub directories.

I also tried messing with find.

I wasn't able to figure this out.

What am I doing wrong?

Thanks.

link|improve this question

80% accept rate
The 41 needs to be in the file name, or its content? – Daniel Beck May 31 '11 at 6:51
@Daniel In the contents, sorry if it wasn't clear. – alex May 31 '11 at 6:53
feedback

2 Answers

up vote 4 down vote accepted

You need both.

find . -name '*.php' -type f -exec grep -q 41 {} \; -print
link|improve this answer
+1 Ignacio. Is it possible for it show the excerpt of the file where it found the substring, like grep normally does? Thanks. – alex May 31 '11 at 6:59
Sure. Just remove -q and the -print predicate. – Ignacio Vazquez-Abrams May 31 '11 at 7:00
Thank you. I really need to learn more about these things... – alex May 31 '11 at 7:01
As you do, you'll learn why find . -name '*.php' -type f -print0 | xargs -0 fgrep 41 -- is the next thing that people will suggest to you. – JdeBP May 31 '11 at 8:37
No need for xargs; BSD find understands -exec ... +. – Ignacio Vazquez-Abrams May 31 '11 at 8:38
feedback

If you use ack you can do ack 41 --php.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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