LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extract each file from TAR archive separately (https://www.linuxquestions.org/questions/linux-newbie-8/extract-each-file-from-tar-archive-separately-926334/)

zx_ 01-29-2012 01: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`):

Code:

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 01: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 01: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 02:56 PM

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
Only that for some reason is much slower then, i.e.

Code:

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

spoore 01-29-2012 03: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
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.

spoore 01-29-2012 03: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 03:32 PM

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

Quote:

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


Cheers

spoore 01-29-2012 04: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.

Regards


All times are GMT -5. The time now is 07:41 PM.