Decompress and extract a tgz or tar.gz archive in a single step

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: UNIX  

Compressed UNIX tar archives (typically tar.gz or .tgz extensions) can be extracted in a single command. This is faster and works with less disk space available.


To extract the compressed tar archive target.tar.gz into the current working directory with:

gzip -dc target.tar.gz | tar xf -

If the file was compressed with bzip2 (i.e., .tar.bz2), you can substitute the bzip2 command for gzip.

If the archive was compressed with the UNIX compress command (archive ending in .Z) you can use:

zcat target.tar.Z | tar xf -

 

10 Comments -


  1. Jeff said on October 3, 2008

    this is easier:

    # tar xvzf target.tar.gz

  2. Quinn McHenry said on October 3, 2008

    Oh, yeah, I’ll admit that command is easier, although back in 2003 it only seemed to work in some linux distros. I was using Solaris (probably 7 at the time) and that tar didn’t have that cool option. I don’t have a Solaris box handy now, but I would be surprised if that hadn’t crept its way in yet.

    Should be noted that ‘j’ in place of ‘z’ in your command will extract a bz2 archived tar file. J. Really. I guess all the appropriate letters were on vacation.

  3. Anonymous said on April 29, 2010

    i got following error when i am trying to run the above command.

    /ITS/release/hawai > gzip -dc sx51_wp4_hp_itanium_dev.tar.gz | tar xf -
    tar: ./bin/cob32 – cannot create
    tar: ./bin/cob64 – cannot create
    tar: ./bin/cobffnd32 – cannot create
    tar: ./bin/cobffnd64 – cannot create
    tar: ./bin/cobffnd32_t – cannot create
    tar: ./bin/cobffnd64_t – cannot create
    directory checksum error

  4. JayJay said on June 4, 2010

    Try typing sudo before gzip

  5. perk said on November 28, 2010

    exactelly true ur the best……

  6. Cyril said on February 20, 2011

    I am under Solaris 10. tar command still has no “z” option.

    Your tip comes in handy! Thanks.

  7. Karen said on March 2, 2011

    Your original method was correct. The tar commands on AIX, HP-UX and Solaris do not accept the options z or j.

    For those lazy GNU people, you can actually omit the letter too, as GNU tar will work out what compression algorithm was used. But the original post done this is the most portable and professonal way

  8. Shadur said on March 16, 2011

    You’re not allowed to create a directory bin/cob32 in /ITS/release/hawai.

  9. Anonymous said on May 15, 2011

    Thanks for adding that updated information. 

  10. JEYAPRAKASH said on December 23, 2011

    BOSS VERY VERY THANK IT’S VERY VERY HELPFUL FOR ME
    THAKS ONCE AGIAN

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -