Possible Duplicate:
How can I grep in source files for some text?

What's the command to search for specific text in specific file types, recursively, under the current directory?

link|improve this question
What do you want returned? the line the text is on? the name of the file the text is in? – MaQleod Aug 25 '11 at 21:49
@MaQleod: That would be good (filename + linenumber + snippet), if that's possible. – Phillip Aug 25 '11 at 21:51
@slhck: ack-grep is not installed; I don't have the permissions to install it. – Phillip Aug 25 '11 at 21:53
1  
You wouldn't need ack-grep, you can use the examples in the question as a started (just like I said), i.e. the find | xargs | grep lines. For the specific output, man grep is always useful to read. – slhck Aug 25 '11 at 21:55
@slhck: Thanks, so I did grep -r --include=*.<type> "<string>" ., which worked. Unfortunately, the man page is not very straightforward in mentioning the trailing dot. – Phillip Aug 25 '11 at 22:03
show 1 more comment
feedback

closed as exact duplicate by slhck, Randolph West, techie007, Nifle, Sathya Aug 27 '11 at 12:25

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

2 Answers

find . -type f -name '*.extension' | xargs grep "string"

This command runs find on the local directory (.) for any files with names matching the pattern *.extension, then runs grep for "string" on the results of the find. Note that find is inherently recursive. As long as you can differentiate the files that you want from the files you don't based on name, this should work for you.

link|improve this answer
This breaks with filenames containing spaces, you should use the print0 option to be safer. – slhck Aug 25 '11 at 22:37
feedback

To list the file name, line number in specified file and line the text appears:

for x in *.xxx; do [ -r $x ] | cat $x | grep -n TEXT | xargs printf "$x:%s\n";done

It will only run in the current directory, but it will format it nicely for you.

link|improve this answer
feedback

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