Extracting one or more files from compressed tar archive

January 26, 2008

As a oracle DBA you will come across extracting files from compressed tar archive files like *.gz/*.Z normally compressed through gzip or UNIX compress utility.  You can uncompress them and extract the tar file.  But it is a two step process and you need more diskspace to do this (twice the amount nearly) .  Instead you can do them in one step. 

How to do this with gzip is clearly demonstrated in here in this URL http://www.sron.rug.nl/didac/didac_tips.html.

gzip -dc t.tar.gz | tar -tvf -

That saves you lot of disk space. 

 Now lets try the same with UNIX uncompress utility.

I have 3 text files and I add them to t.tar
$ ls -ltr
total 146
-rw-r--r-- 1 oracle dba 6 Jan 26 05:28 t1.txt
-rw-r--r-- 1 oracle dba 6 Jan 26 05:28 t2.txt
-rw-r--r-- 1 oracle dba 6 Jan 26 05:28 t3.txt
$ tar -cvf t.tar *.txt
a t1.txt 1K
a t2.txt 1K
a t3.txt 1K

Now compress them using UNIX compress command

$ compress t.tar
$ ls -ltr
total 148
-rw-r--r-- 1 oracle dba 296 Jan 26 05:31 t.tar.Z

Try the uncompress command

$ uncompress -c t.tar.Z | tar -tvf -
tar: blocksize = 8
-rw-r--r-- 101/201 6 Jan 26 05:28 2008 t1.txt
-rw-r--r-- 101/201 6 Jan 26 05:28 2008 t2.txt
-rw-r--r-- 101/201 6 Jan 26 05:28 2008 t3.txt

Yes!! it really works!  If you want to extract just one file, you can do the below

$ uncompress -c t.tar.Z | tar -xvf - t1.txt
tar: blocksize = 8
x t1.txt, 6 bytes, 1 tape blocks
$ ls -ltr
total 144
-rw-r--r-- 1 oracle dba 6 Jan 26 05:28 t1.txt
-rw-r--r-- 1 oracle dba 296 Jan 26 05:31 t.tar.Z