Unix/Linux — Find files that contain a string

By Rares Vernica

To find the files that contain a string we can use:

find . -exec grep -l “string to find” {} \;

This starts the search from the current directory, looks for files that  contain the specified string, and then it prints their names.

4 Responses to “Unix/Linux — Find files that contain a string”

  1. radu Says:

    If you also want to print an excerpt of the matched text, change the command to:

    find . -exec grep -H “string to find” {} \;

    In both cases, for faster results (does not spawn multiple grep instances), reverse grep with find like this:

    grep -l “string to find” `find .`

    or

    grep -H “string to find” `find .`

  2. danielneri Says:

    great, most helpful!

  3. mojowen Says:

    thanks dude!

  4. mojowen Says:

    I’ve been liking the original better:

    find . -exec grep -l “string to find” {} \;

    I tend to get an error that says “Argument List Too Long” when I use Radu’s.

Leave a Reply

You must be logged in to post a comment.