Extracting file from .tar.gz file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting file from .tar.gz file
# 1  
Old 07-24-2013
Extracting file from .tar.gz file

Hi,

I receive a huge .tar.gz file that may reach gega's , and i try as much as i can to avoid disk space issues

Is there a way to extract from such a big file withought the need to decompress the file? given that i know the filesnames format that i will need to extract from the tar file: they are start with 'D' and end with 'T' , for e.g D1fdS34T

My big issue is the need to decompress first as it eats the space !

Thanks in advance,
Eman El Badawy
# 2  
Old 07-24-2013
You don't need to store the entire decompressed file on disk.

Code:
gunzip -c < file.tar.gz | tar -xf - filename1 filename2 filename3

Or on some systems, just
Code:
tar -zxf file.tar.gz filename1 filename2 filename3

As far as I know you can't extract all files beginning with a certain string though.

It has to scan the whole file to find the ones you want.
# 3  
Old 07-24-2013
Quote:
Originally Posted by Corona688
You don't need to store the entire decompressed file on disk.

Code:
gunzip -c < file.tar.gz | tar -xf - filename1 filename2 filename3

Or on some systems, just
Code:
tar -zxf file.tar.gz filename1 filename2 filename3

As far as I know you can't extract all files beginning with a certain string though.

It has to scan the whole file to find the ones you want.
True, but you can make one pass to get the names of the files in the tar archive and save the names of the files you want using grep (or some other tool) and then extract the files you want on a second pass. The 1st pass would be something like:
Code:
tar -ztf file.tar.gz | grep -E '(^|/)D.*T(/|$)'

Again, this does not save the entire uncompressed archive to disk. And, of course, if you try this and find that it gives you the list of files you want, you could try:
Code:
tar -zxf file.tar.gz $(tar -ztf file.tar.gz | grep -E '(^|/)D.*T(/|$)')

as long as the list of filenames to be processed doesn't cause that command line to overflow your system's ARG_MAX limit.
# 4  
Old 07-24-2013
Quote:
Originally Posted by Don Cragun
Code:
tar -ztf file.tar.gz | grep -E '(^|/)D.*T(/|$)'

Again, this does not save the entire uncompressed archive to disk. And, of course, if you try this and find that it gives you the list of files you want, you could try:
Code:
tar -zxf file.tar.gz $(tar -ztf file.tar.gz | grep -E '(^|/)D.*T(/|$)')

as long as the list of filenames to be processed doesn't cause that command line to overflow your system's ARG_MAX limit.
It looks to me like your grep regular expression is underspecified, because D.*T is allowed to span pathname components. [^/] seems a better choice.

Regarding avoiding ARG_MAX, one filename per line can be read from a file, using GNU tar's -T or BSD tar's -I. Perhaps other implementations offer similar functionality.

To the OP:
When asking for help with tar, always specify the implementation you're using (or at least the operating system). Aside from core functionality, tar isn't well standardized.

If you're using GNU tar (untested):
Code:
tar xzf file.tar.gz --wildcards --no-anchored 'D*T'

Regards,
Alister

Last edited by alister; 07-24-2013 at 04:40 PM..
# 5  
Old 07-24-2013
What about:
Code:
pax -rzf ./bla.tar.gz 'D*T'

# 6  
Old 07-24-2013
Quote:
Originally Posted by Scrutinizer
What about:
Code:
pax -rzf ./bla.tar.gz 'D*T'

The pattern, D*T, matches against the entire pathname, not just the basename. Also, / need not be matched explicitly, so the pattern can span multiple components. Whether this is a dealbreaker depends on information we do not have. It wouldn't be a problem if the archive is guaranteed to always be a simple, flat list of files.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 07-25-2013
Ok, good point. So depending on the content we would need:
Code:
pax -rzf ./bla.tar.gz 'D*T'

or
Code:
pax -rzf ./bla.tar.gz '*/D*T'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Uncompressing but not extracting tar.Z file

Hi I have a few hundred files with extension .tar.Z. These files were archived (tar) and compressed (Z) on a UNIX system. I need to unzip them but not extract them. In other words they need to go to .tar extension. I would like to do this on my MAC or on a windows pc. I do not have a UNIX... (3 Replies)
Discussion started by: kalbano
3 Replies

2. Shell Programming and Scripting

Problem extracting zipped tar file

I was extracting the zipped tar file with the command gzip -dc Sample.tar.gz |tar xf - The tar file contained many delimited files; but lately they changed the structure of the tar file with another folder. So now all the delimited files are inside a folder called "Folder1" and the folder... (6 Replies)
Discussion started by: asandy1234
6 Replies

3. UNIX for Advanced & Expert Users

Issue in extracting Tar and Zipped file.

Hi, I want to tar files and zip them in order to clean up space in directory. I have files like /path/file1 /path file2. What I am trying to do is: Option 1: tar -cvf /path/file1 /path file2 | gzip > test.tar.gz I got the file created. But while trying to extract the Tar and zipped file, I... (1 Reply)
Discussion started by: Quesemail
1 Replies

4. UNIX for Dummies Questions & Answers

Extracting specific files from a tar file in HP-UX

I have tried: tar -xfv mytarfile.tar archive/tabv/* tar -xfv mytarfile.tar --wildcards 'archive/tabv/*' tar -xf mytarfile.tar -v --wildcards 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards --no-anchored 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards `archive/tabv/*` and none... (5 Replies)
Discussion started by: zapper222
5 Replies

5. Solaris

Extracting file from tar at relative location!!

Hi Gurus, I have a .tar file which required untar to the new location. I list the content with –tvf its listing the files which are inside the tar, when I am extracting he file from tar its working fine, however once I am trying to extract the file at the new location I am unable to do so. I... (11 Replies)
Discussion started by: kumarmani
11 Replies

6. Solaris

option to delete .tar file while extracting

Is there an option in tar which deletes the .tar file as soon as it is successfully extracted. (5 Replies)
Discussion started by: vickylife
5 Replies

7. Shell Programming and Scripting

How to read the content of the particular file from tar.Z without extracting?

Hi All, I want to read the content of the particular file from tar.Z without extracting. aaa.tar.Z contains a file called one.txt, I want to read the content of the one.txt without extracting. Please help me to read the content of it. Regards, Kalai. (12 Replies)
Discussion started by: kalpeer
12 Replies

8. UNIX for Dummies Questions & Answers

Extracting from a tar archive file

Can I extract files from an archive file (tar), where the filename includes the full directory path, to a different directory? For example the archive files may have a filename of /SrcFiles/XXX/filename.dat and I want to extract it to /SrcFiles/YYY/filename.dat. Since the archive file was... (1 Reply)
Discussion started by: nmalencia
1 Replies

9. UNIX Desktop Questions & Answers

Extracting from a tar file

Can I extract files from an archive file (tar), where the filename includes the full directory path, to a different directory? For example the archive files may have a filename of /SrcFiles/XXX/filename.dat and I want to extract it to /SrcFiles/YYY/filename.dat. Since the archive file... (1 Reply)
Discussion started by: nmalencia
1 Replies

10. UNIX for Dummies Questions & Answers

extracting from a tar file

Dear experts I have received a tar file containing several files with full path. Now I need to restore it in another system but when I want to extract files by using tar -xvf tarfile it wants to create all files with full paths again in new system in which I don't have enough previleges. How... (4 Replies)
Discussion started by: Reza Nazarian
4 Replies
Login or Register to Ask a Question