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

closed as unclear what you're asking by Jeff Schaller, John WH Smith, G-Man, mdpc, countermode Jan 13 '17 at 8:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Is this a homework question? Why the requirement for cat? Are you aware that GNU/Linux tar can uncompress and unarchive all by itself? – Jeff Schaller Jan 12 '17 at 19:15

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 -


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

  • 1
    v isn't necessary for the process to complete; it just makes tar verbose. – DopeGhoti Jan 12 '17 at 17:23
  • @DopeGhoti Yep, I like seeing things happening. Will add more info shortly. – Kusalananda Jan 12 '17 at 17:23
  • True, but depending on the amount of files in the tarball, a verbose listing could run longer. – sleepyweasel Jan 12 '17 at 18:00
  • @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. – Kusalananda Jan 12 '17 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.