I find the find . -name "some-file" command very useful to list all files matching some file name in a folder. Is there anything similar I can use to list all files that contains string?

If you needed to find all files in a directory that had a certain string of text in it, what would you use?

link|improve this question

68% accept rate
feedback

2 Answers

up vote 2 down vote accepted

If you don't need to search subdirectories:

grep -l "string" <pattern>

(<pattern> is a file glob pattern, eg *.txt)

link|improve this answer
Just found that out, hehe. Didn't know grep could work on directories as well. And if you add -r it will search subdirectories as well! For example grep -lr "string" . – Svish Apr 22 '10 at 14:01
feedback

Just in case you are using a system with a POSIX strictly compliant grep, ie which doesn't support the -r flag, but you still want to recurse in sub-directories, you can use this command:

find . -type f -name "*.txt" -exec grep -l string {} +
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.