Alessandro C
Alessandro C

Reputation: 3560

Print the first line of each file inside a tar.gz without extracting

I'm looking for a command in order to print the first line of every file contained in a tar.gz archive, without extracting it.

Example:

tar -ztvf MyArchive.tar.gz
-rw-r--r-- root/root 3732541752 2020-04-04 03:24 FILE1.TXT
-rw-r--r-- root/root   90493394 2020-04-04 03:16 FILE2.TXT
-rw-r--r-- root/root  103294570 2020-04-03 21:06 FILE3.TXT
-rw-r--r-- root/root   16865694 2020-04-03 21:07 FILE4.TXT
-rw-r--r-- root/root 13176227988 2020-04-03 23:36 FILE5.TXT

I need to print the first line of each FILE*.TXT inside the tar.gz

How can I achieve this?

Upvotes: 1

Views: 954

Answers (2)

Philippe
Philippe

Reputation: 26422

Try this:

tar zxf MyArchive.tar.gz --to-command="head -n 1"

This command takes files in the tar individually and feeds them into the command "head -n 1".

Upvotes: 1

Ratnam
Ratnam

Reputation: 11

You could achieve using tar and for loop commands.

for i in $(tar -ztvf MyArchive.tar.gz|grep -i file|awk '{print $NF}')

do

tar xfO MyArchive.tar.gz $i|head -1

done

Using "tar xfO MyArchive.tar.gz filename" to read the content of files inside tar.gz

Upvotes: 1

Related Questions