2

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?

Peter
  • 1,224
  • 3
  • 16
  • 28
  • 1
    possible duplicate of [Grep Search all files in directory for string1 AND string2](http://stackoverflow.com/questions/4275900/grep-search-all-files-in-directory-for-string1-and-string2) – IMSoP Jan 18 '14 at 20:18

2 Answers2

7

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

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

Edit

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"
phs
  • 10,687
  • 4
  • 58
  • 84
  • Oh - I've wrote something wrong. Especially your 2nd line is extremely helpful. Thanks a lot and sorry for confusion. – Peter Jan 27 '14 at 21:02
0
egrep -irl "hello" * |egrep -irl "peter" \`cat -`

This will search recursively in all subfolders and will match regex within the quotes

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
akkig
  • 116
  • 7
  • Does not work. One file should have been found. But I just get "File or path not found". – Peter Jan 22 '14 at 22:18