Find all files with two words

Suppose we want to find all files that contain two words.

find . -type f | xargs grep -l "word1" | xargs grep -l "word2"

How this works

  1. find” will return a list of all files
  2. this list is piped into “xargs
  3. xargs” will pass each item in the list to its own function – in this case, the “grep” command
  4. the “-l” option on the “grep” will return just the filenames of those files containing the given word
  5. this is piped back into the second instance of “xargs” to filter the second word
  6. the result is a list of files containing both words

Leave a Reply