How can I perform a second grep on the files returned from a previous grep?

Solution 1:

grep -lZ "first string" * | xargs -0 grep -l "second string"
  • First grep will return the files containing first string.

  • Second grep will do the same for second string, but over the results from the first grep.

  • The -Z argument to grep and the -0 argument to xargs work together to enable support for filenames that include spaces.


Edit - thanks to Ajedi32:

xargs lets you use the results from a command as the arguments to another.

From the xargs's Wikipedia article, xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input.