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

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 -


About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

  • Jeff

    this is easier:

    # tar xvzf target.tar.gz

    • perk

      exactelly true ur the best……

  • 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.

    • Karen

      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

      • Anonymous

        Thanks for adding that updated information. 

  • Anonymous

    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

    • JayJay

      Try typing sudo before gzip

    • Shadur

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

  • Cyril

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

    Your tip comes in handy! Thanks.