Search code examples
grepgzipzcat

Search through *.gz files with keeping the file name


Say I have multiple .gz files that I want to search a keyword in them. I can do this by piping zcat result to a grep like this:

zcat some.file.* | grep "keyword_1" | ... | grep "keyword_n"

The output of this command though will be just the matching line and won't have the file name in it. Is there any way I can attach the file name to the zcat output?


Solution

  • Try zgrep instead of zcat:

    zgrep -H keyword some.file.*
    

    And if you want to use egrep to get pattern matching:

    export GREP=egrep
    zgrep -H -e "(keyword1|keyword2)" some.file.*