Find all files containing a given string

A quick one-liner to recursively search files for a given text string.

Open up a terminal session and enter the following - replacing "foo" with the text to search for.

find . -exec grep -l "foo" {} \;

You can also limit the search to files with a particular extension (e.g. HTML or CSS). The first form will only return files in the current directory

find *.html -exec grep -l "foo" {} \;

Alternatively you can search recursively while matching filenames using the -name parameter:

find . -name *.html -exec grep -l "foo" {} \;

You can pass other options to find including for example -mtime -1 to specify only files modified within the past day.

You can also replace . with the name of any other folder to search:

find /var/www -name *.html -exec grep -l "foo" {} \;

This will search /var/www recursively for files named *.html and containing foo.


Get regular Shell tips & tutorials direct to your Inbox.

Discussion

Related posts