tutorials bash

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.

python
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

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

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

python
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:

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

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

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
More info Get the book

Downloadable ebook (PDF, ePub) & Complete Source code

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount on all books and courses.

[[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount on all books and courses.