I need to grep some files to see which ones contain a certain word:
grep -l word *
Next I need to grep that list of files to see which ones contain a different word. The easy way would probably be to write a script but I don't know quite how to work it.
Best Answer
Assuming none of the file names contain whitespace, single quote, double quote or backslash characters (or start with - with GNU
grep
), you can do:Xargs will run the second grep over each file from the first grep.
With GNU
grep
/xargs
or compatible, you can make it more reliable with:Using
-Z
makesgrep
print the file names NUL-delimited so it can be used withxargs -0
. The-r
option toxargs
avoids the secondgrep
being run if the firstgrep
doesn't find anything.