0

I have multiple *.tar.gz files. They all contain files with file name a, b, and c. Is there a way to grep string 'foo' only from file b under these multiple *.tar.gz files without extracting the files.

2 Answers2

6

Assuming that by "without extracting" you mean "without saving extracted files to disk":

for file in *.tar.gz ; do
    tar xzOf $file b | grep --label=$file/b -H foo
done

tar options:

x extract
z gunzip before extracting
O dump to stdout rather than creating file (capital letter oh, not number zero)
f from specified tar file

grep options (suggested by JJoao)

--label=... use specified label as the filename
-H print filename for each match

user4556274
  • 8,995
  • 2
  • 33
  • 37
1

The command zgrep works on .tar.gz files without extracting the contents.

Jesusaur
  • 354