Extract tar.gz file in Linux

Filed Under: UNIX/Linux
Extract Tar Gz File

We’ll look at how we can extract a tar.gz file in Linux.

A tar.gz is an archive file that contains files of other types. It acts as an intermediate storage file across a network. It also compresses all these files compactly. This makes it easy to send these files.

There are two steps involved in this process.

  • The sender compresses all necessary files into the archive container, and sends it across a network.
  • The receiver, after getting the file, can now unpack the contents from the tar.gz archive.

We look at how we can extract this archive file format in Linux.

Extract the tar.gz file

We can use the tar command to unpack such a file. If this file is compressed using a gzip compressor, the following command applies:

tar -xzf filename.tar.gz

Here, filename.tar.gz is the archive that you wish to unpack.

This means, that we instruct the tar command to:

  • x -> Extract the files
  • z -> This filters the archive using gzip
  • f -> Use an archive file

The archive name must come immediately after the f option.

If we want tar to display more information about the files, we can also specify the -v verbose option.

tar -xvzf filename.tar.gz
Extract Gzip File
Extract Gzip File

Extract without using gzip

For some reason, if this doesn’t work, try typing the following command, without using gzip.

tar -xf filename.tar.gz
Extract Without Gzip
Extract Without Gzip

As you can observe, since we packed our archive without the -z option (without gzip), we cannot use the -z option when we unpack it. So, we have to un-compress it normally.

Extract to a specified Directory

We can extract the archive to any directory by specifying the -C option. This tells tar to change the directory before unpacking.

tar -xvzf filename.tar.gz -C ~/path/to/extract
Extract To Directory
Extract To Directory

You can see that the unpacked contents are indeed stored to ~/random.


Hopefully, this article clears any lingering doubts about how you could extract a tar.gz file in Linux. Otherwise, feel free to ask any questions in the comment section below!


Generic selectors
Exact matches only
Search in title
Search in content