Uncompress and unarchive the .tar.gz file using cat, tar and gzip on one command line

12,107

No need for cat or gzip:

$ tar xvzf archive.tar.gz

The option string tells tar to extract (x) in verbose mode (v) the compressed (z) archive following the f flag.

Using all of cat, tar and gzip (which is silly, don't do this):

$ cat archive.tar.gz | gzip -d -c | tar xvf -

or

$ gzip -d -c archive.tar.gz | cat | tar xvf -

When given - as the filename, tar will read the archive from standard input.

The last two examples suffer from what's commonly called "Useless use of cat", since the only thing cat does is to shuffle data to the next part of the pipeline.

A slightly better version, without the cat:

$ gzip -c -d archive.tar.gz | tar xvf -

This may actually be a useful thing to know how to do on a system where tar doesn't know how to handle compressed archives.

The archaic option string that I've used for tar above, without the dash (-) in front of it, comes from a time before the dash was commonly used for specifying command line options. Other utilities that do not use regular option syntax includes dd and mt, but whereas most implementations of tar today understands the newer dash options, dd usually doesn't. The mt command (which controls magnetic tape operations) has acquired some dash options (especially on Linux), but the only non-optional part of the command line is still the command that you'd like to perform on the drive, such as rewind or eof.

Share:
12,107

Related videos on Youtube

Matej Gomboc
Author by

Matej Gomboc

Updated on September 18, 2022

Comments

  • Matej Gomboc
    Matej Gomboc almost 2 years

    How can I uncompress and unarchive a .tar.gz file using cat, tar and gzip on one command line? Is this even possible?

    • Jeff Schaller
      Jeff Schaller over 7 years
      Is this a homework question? Why the requirement for cat? Are you aware that GNU/Linux tar can uncompress and unarchive all by itself?
  • Pankaj Goyal
    Pankaj Goyal over 7 years
    v isn't necessary for the process to complete; it just makes tar verbose.
  • Kusalananda
    Kusalananda over 7 years
    @DopeGhoti Yep, I like seeing things happening. Will add more info shortly.
  • sleepyweasel
    sleepyweasel over 7 years
    True, but depending on the amount of files in the tarball, a verbose listing could run longer.
  • Kusalananda
    Kusalananda over 7 years
    @sleepyweasel Whether this matters or not is a question of personal taste. Personally, I'd rather look at a long listing of files than save a few seconds of time because 1) I can see things are being extracted where I expect them to be extracted, and ) I'm never in that kind of hurry.