Peter Peter - 11 months ago 46
Linux Question

How to combine two search words with "grep" (AND)

With grep, I can find a word within 50 files in a local folder, i.e.:

grep -i "hello" *.html

But how can I find files that contain TWO words? Example: I would like to find all files, that contain Word "hello" AND word "peter". How can I combine two grep's?

phs phs
Answer Source

To see the files containing both words (possibly on different lines), use -l and xargs:

grep -il "hello" *.html | xargs grep -il "peter"


If your files have spaces in their names, then we need to be a little more careful. For that we can use special options to grep and xargs:

grep -ilZ "hello" *.html | xargs -0 grep -il "peter"