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`): Code:
tar -tf archive.tar.gz | xargs -0 tar -Oxf archive.tar.gz | grep something Am I on wrong path? Is there easier way to do this? |
It could depend on your version of tar but, you could try this:
tar ztf archive.tar.gz |xargs grep something |
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 |
Here is my ugly but working example:
Code:
tar -tf archive.tar.gz > /tmp/tmplist && while read line; do echo "$line" && tar -Oxzf archive.tar.gz "$line" | grep something ; done < /tmp/tmplist Code:
tar -tf archive.tar.gz > /tmp/tmplist | tar -Oxzf archive.tar.gz -T /tmp/tmplist | grep something |
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 OUTPUT="" if [ $(echo $FILE|grep "/$"|wc -l) -gt 0 ]; then continue fi OUTPUT=$(tar zOxf archive.tar.gz $FILE|grep something) if [ -n "$OUTPUT" ]; then IFS=" " for LINE in $(echo "$OUTPUT") do IFS="" echo "$FILE: $LINE" done fi done If you're looking for a one liner though, I'd dig into xargs options some more and see what options there may be. |
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.
|
Excellent. Thanks :)
It simplifies things a lot, and works as expected: Quote:
I wouldn't have known also about `tar -v` switch, which lists "extracted" filenames Cheers |
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.
Regards |
All times are GMT -5. The time now is 07:41 PM. |