47

When I download a .tar.gz file, I open it with two commands, first gunzip and then tar.

Is it possible to open it with just one command?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
flybywire
  • 261,858
  • 191
  • 397
  • 503

8 Answers8

87
tar xzf file.tar.gz

The letters are:

  • x - extract
  • z - gunzip the input
  • f - Read from a file, not stdin
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 3
    This is correct. The only thing that I would add is that z usually only works on gnu tar. The typical UNIX tar won't have this in my experience at least. – Jon Mar 16 '09 at 16:19
  • 1
    `tar xf file.tar` if you have `file.tar`. – mrgloom Mar 01 '19 at 12:20
  • If you are working with a very large .tgz file, and unzipping it to the same file system will result in an out of disk space error, you can add `--directory /existing/target/directory` where `/existing/target/directory` is on another file system that has sufficient space. This is especially handy if said destination file system can't handle files as large as the particular .tgz file. In this case adding `v` to the options as mentioned in @WiseTechi 's answer (and other answers) can be useful so you know it is actually working as desired. – hlongmore Aug 31 '20 at 06:54
12

You can use tar with a "z" argument

tar xvfz mytar.tar.gz
WiseTechi
  • 3,528
  • 1
  • 22
  • 15
4

If you don't have gnu tar it is still possible to open the file in a single step (although technically still two commands) using a pipe

zcat file.tar.gz |tar x
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
2

The only thing that I would add is that z usually only works on gnu tar. The typical UNIX tar won't have this in my experience at least. – Jon Mar 16 at 16:19

The z option works well on my OS X v10.5 (Leopard) as well.

When it comes to memorizing, I think it´s easy to think of what you want and not just some letters.

  1. If you want to create an archive, then c will be the first option, else x will be the first option if you want to extract.
  2. If you want to compress/decompress with the gzip/gunzip program, then the next option should be z for zip. (All archives ending with .gz must be unzipped with the z option)
  3. The last mandatory option is f for the file.

Then you usually end up with these two commands:

  • tar czf file.tar.gz /folder_to_archive/*
  • tar xzf file.tar.gz
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Espen
  • 10,545
  • 5
  • 33
  • 39
2

How do I extract a gz file?

Use the guzip command as follows:

gunzip file.gz

Or

gzip -d file.gz

How do I extract a tar.gz or .tgz file?

Files with extension tar.gz or .tgz are tar files compressed with gzip. On Unix system extract them with following command:

gunzip < file.tar.gz | tar xvf -
gunzip < file.tgz | tar xvf -

If you have GNU tar (Linux system) you can use the z option directly:

tar xvzf file.tar.gz
tar xvzf file.tgz

To read more, see Linux / UNIX command to open .gz files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shanewaj
  • 2,078
  • 1
  • 20
  • 16
1

Untar Single file from tar File compressed file

To extract a single file called video.mpeg from video.tar use the following command as given below example.

# tar -xvf video.tar video.mpeg

video.mpeg

Extracting Single file from tar.gz File

Use the following command to extract a single file codefile.xml from websitecode.tar.gz archive file

# tar -zxvf websitecode.tar.gz code.xml

code.xml

Extracting Single file from tar.bz2 File

Use the command to extract single file file.html from websitecode.tar.bz2

# tar -jxvfwebsitecode.tar.bz2 home/website/file.html

/home/website/file.html

Also you can read more about all the tar commands at This link explains all the useful tar commands available in linux

0

On Windows, try the tartool utility.

It's free and the code is open source and uses the SharpZipLib library.

Disclaimer: I am the author of this utility.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
senthil
  • 339
  • 3
  • 8
0

Nowadays tar decompresses automatically if needed. It is enough to write:

tar xf file.tar.gz

Mnemonic: extract file

stackprotector
  • 10,498
  • 4
  • 35
  • 64