Search code examples
bashgunzipzcat

First line from multiple gz files


I know that there are multiple ways to read the first line from a gz file, for example:

gzcat abc.gz | head -n 1

or

gunzip -c abc.gz | awk 'NR==1 {print; exit}'

However, I have a directory with many, many gz files, and I want to get the header for each file (*.gz) with the filename next to it, like:

abc.gz: This is the first line of abc.txt def.gz: This is the first line of def.txt

Is there a quick way to do this?

[EDIT:] Thanks for the help! The answers are close, but I just want the filename next to each first line output so I know which line goes with which file.


Solution

  • For "GNU" find use:

    find . -name "*.gz" -exec sh -c "echo -n '{}: '; gzcat {}|head -1" \;