This question already has an answer here:

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.

marked as duplicate by Gilles, GAD3R, Anthon, Eric Renouf, mdpc Sep 15 '16 at 21:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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

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


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.