Extracting one or more files from compressed tar archive

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

One Response to “Extracting one or more files from compressed tar archive”

  1. Philip Says:

    Mani,
    I found the command “uncompress -c t.tar.Z | tar -xvf -” extremely useful. I had exactly the situation you describe: I had room to uncompress a large file, but didn’t have room to then untar the uncompressed file. Doing the uncompress and untar in a single operation is far quicker too!

    I first created a symbolic link to redirect the contents of the tar file to where I needed them (the files in the tar had the full pathname of where they were copied from, but this was not the same as where I wanted to retore them). Then I ran the “uncompress -c t.tar.Z | tar -xvf -” command.

    Many thanks for this suggestion,
    Philip

Leave a Reply