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?


If you don't need to search subdirectories:

grep -l "string" <pattern>

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

  • 1
    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

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 {} +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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