-   Linux - Newbie (
-   -   Extract each file from TAR archive separately (

zx_ 01-29-2012 02:05 PM

Extract each file from TAR archive separately
I want to use grep to search inside tar gziped archive and output the name of the file with match along with matched lines.

I thought to use `tar -tf archive.tar.gz` to list files then echo filename to terminal and extract then do grep search, but can't seem to find way as I've never used this `tar` tool and it has so many switches

I tried i.e. this (I didn't even get to `echo`):


tar -tf archive.tar.gz | xargs -0 tar -Oxf archive.tar.gz | grep something
but second `tar` command does not understand what xargs passes to it.

Am I on wrong path? Is there easier way to do this?

spoore 01-29-2012 02:19 PM

It could depend on your version of tar but, you could try this:

tar ztf archive.tar.gz |xargs grep something

zx_ 01-29-2012 02:27 PM

It's: tar (GNU tar) 1.26

I'm not sure about your idea, but it seems like you are suggesting to list archive and pass filenames from archive to grep?
If so, I can't think how can I accomplish anything with that

zx_ 01-29-2012 03:56 PM

Here is my ugly but working example:


tar -tf archive.tar.gz > /tmp/tmplist && while read line; do echo "$line" && tar -Oxzf archive.tar.gz "$line" | grep something ; done < /tmp/tmplist
Only that for some reason is much slower then, i.e.


tar -tf archive.tar.gz > /tmp/tmplist | tar -Oxzf archive.tar.gz -T /tmp/tmplist | grep something

spoore 01-29-2012 04:19 PM

oops...duh...That's not right. Yes, what you described is exactly what that does. I did a quick test in a directory that contained the expanded tar.gz so it "appeared" (incorrectly) to do what you wanted.

Looking again, I'm pretty sure your tar options are correct but, there may be something with xargs to change. This doesn't do exactly what you're looking for but, maybe it'll help somewhat:

tar ztf archive.tar.gz |xargs -I{} tar -zvOxf archive.tar.gz {} | grep something

It's not perfect as it will print out the filenames of everything even if it's not found but, it's may help.

Or, you could just hack together a script:

for FILE in $(tar ztf archive.tar.gz); do
if [ $(echo $FILE|grep "/$"|wc -l) -gt 0 ]; then
OUTPUT=$(tar zOxf archive.tar.gz $FILE|grep something)
if [ -n "$OUTPUT" ]; then
for LINE in $(echo "$OUTPUT")
echo "$FILE: $LINE"

If you're looking for a one liner though, I'd dig into xargs options some more and see what options there may be.

spoore 01-29-2012 04:21 PM

Oh, and I've got the same version of tar on a fedora 15 box. And sorry about the formatting for the shell code above.

zx_ 01-29-2012 04:32 PM

Excellent. Thanks :)
It simplifies things a lot, and works as expected:


Originally Posted by spoore (Post 4587636)
tar ztf archive.tar.gz |xargs -I{} tar -zvOxf archive.tar.gz {} | grep something

Interestingly, `tar tf ...` lists files (which are with spaces) and thats why I used `xargs -0 ...` but for some reason there is no need to

I wouldn't have known also about `tar -v` switch, which lists "extracted" filenames


spoore 01-29-2012 05:53 PM

I was going to say I should have refreshed my browser before my response. If I had though, I may not have sent that. Glad it helped.


All times are GMT -5. The time now is 09:36 PM.