Unix/Linux — Find files that contain a string

by

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.

Other Unix/Linux not so frequently asked questions.

6 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.

  5. Oren GoogleMoggle Says:

    how can one find a row that contains only a sting and the carriage return/line feed

    for example i want to find <? (which is an obsolete php directive) and prevent the search from returning rows containing <?php

    Any ideas ?

  6. Sudheer Kumar Says:

    we can also use this: find . -name ‘*’ | xargs grep -l ‘string to find’

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: