How to find multiple strings in files?

POSIXly, using grep with -E option:

find /var/www/http -type f -exec grep -iE 'STRING1|STRING2' /dev/null {} +

Or -e:

find /var/www/http -type f -exec grep -i -e 'STRING' -e 'STRING2' /dev/null {} +

With some implementations, at least on GNU systems, OSX and FreeBSD, you can escape |:

find /var/www/http -type f -exec grep -i 'STRING1\|STRING2' /dev/null {} +

For ease of maintenance (if your list of strings to search may change in the future), I would put the patterns in a file (eg. patterns.txt) and use the -f switch (-R is unnecessary if you are restricting find to files; -H will give you the file name in case there is only one; -F causes grep to treat the patterns you are searching for as strings, and not regular expressions, which is usually what you want):

find /var/www/http -type f -exec grep -iHFf patterns.txt {} +

What's wrong with just using egrep?

egrep -r 'string1|string2|string3' /var/www/http

Tags:

Grep

Find