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
- “find” will return a list of all files
- this list is piped into “xargs”
- “xargs” will pass each item in the list to its own function – in this case, the “grep” command
- the “-l” option on the “grep” will return just the filenames of those files containing the given word
- this is piped back into the second instance of “xargs” to filter the second word
- the result is a list of files containing both words