LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-29-2012, 01:05 PM   #1
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Rep: Reputation: Disabled
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?

Last edited by zx_; 01-29-2012 at 01:06 PM.
 
Old 01-29-2012, 01:19 PM   #2
spoore
LQ Newbie
 
Registered: Aug 2004
Posts: 23

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

tar ztf archive.tar.gz |xargs grep something
 
Old 01-29-2012, 01:27 PM   #3
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-29-2012, 02:56 PM   #4
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-29-2012, 03:19 PM   #5
spoore
LQ Newbie
 
Registered: Aug 2004
Posts: 23

Rep: Reputation: 2
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.
 
1 members found this post helpful.
Old 01-29-2012, 03:21 PM   #6
spoore
LQ Newbie
 
Registered: Aug 2004
Posts: 23

Rep: Reputation: 2
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.
 
Old 01-29-2012, 03:32 PM   #7
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Excellent. Thanks
It simplifies things a lot, and works as expected:

Quote:
Originally Posted by spoore View Post
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

Last edited by zx_; 01-29-2012 at 03:43 PM.
 
Old 01-29-2012, 04:53 PM   #8
spoore
LQ Newbie
 
Registered: Aug 2004
Posts: 23

Rep: Reputation: 2
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] tar fails to extract archive containing special characters shadowbox12 Linux - General 2 09-14-2011 06:05 AM
How to extract too large archive (35 GB) .tar file Bharat Kumar pankaj Linux - Server 10 08-19-2008 07:35 AM
Piping tar bzcat to add a file to a tar.bz2 archive DaveQB Linux - Software 0 06-02-2008 08:28 PM
How can I extract a file from iso archive kmichal2 Linux - Software 8 12-14-2004 05:48 AM
Tar gives error when creating a tar file archive davidas Linux - Newbie 10 04-13-2004 12:35 AM


All times are GMT -5. The time now is 01:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration