How to Extract tar.gz File in Linux by Using the Command Line

How to Extract tar.gz File in Linux by Using the Command Line

This article will learn how to extract/untar tar.gz files in Linux systems through the command line using the tar command.

Many of the downloadable Linux/Unix files found on the internet are compressed using a tar.gz format. Therefore, knowing how to open or untar tar.gz files is very useful.

The name “Tar” stands for “Tape Archiver” because it was used to place data on storage tapes when tar was invented. A .tar.gz file is nothing but an archive. The tar program takes one or more files and “wraps” them into a self-contained file.

To untar tar.gz files means to extract the contents of the tar file (also known as a tarball). Additionally, if you want to learn how to create tar.gz files in Linux, check out our excellent guide, “How to Create tar.gz Archive Using the tar Command on Linux.”

People new to the .tar format usually equate it to a .zip archive, but a tar archive is not compressed. Tar collected all the files into one package, but the files can be compressed with separate utilities.

The most often used algorithm for compressing tar files is Gzip. By convention, the name of a tar archive compressed with gzip becomes .tar.gz or .tgz.

To put it simply: a file that ends in .tar.gz is just a .tar archive compressed with gzip.

How to Extract a tar.gz File

Most Linux distributions come with the tar command pre-installed by default.

To untar tar.gz file, enter the following:

tar xvzf file.tar.gz

Let’s break down this syntax. Here is what each parameter in that command means:

  • x: This option tells tar to extract the files.
  • v: Verbose output shows you all the files being extracted.
  • z: Tells tar to decompress the archive using gzip.
  • f: This must be the last flag of the command. It tells tar the name and path of the compressed file.

How to Extract a tar.gz File to a Different Directory

To uncompress the tar.gz file and put resulted files in a different directory, say /tmp/archive, you need to add a -C option at the end of the command:

tar xvzf file.tar.gz -C /tmp/archive

The -C option is used to specify a different directory other than the current working directory.

View a List of Files within an Archive

Sometimes you need to view the content of a tar file as it collects many files and ensures if a specific file is present. 

To view a detailed table of contents for archive called data.tar.gz, use the following syntax:

tar tf data.tar.gz
View a List of Files within tar.gz Archive

Where:

  • t: Used to list the content of the tar file
  • f: Commanding the utility to use the file mentioned in the following argument

You can also get detailed standard output by using the v (verbose) option.

tar tvf data.tar.gz
How to Extract tar.gz File in Linux by Using the Command Line

Summary

Now you know how to untar a tar.gz file in Linux. Perhaps you might also be interested in learning how to unzip files in Linux.

For more about tar command in Linux, consult its manual page.

If you find this article helpful or have additional ideas, please use the comment box below.

Bobby Borisov
Bobby Borisov

Bobby is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

Your email address will not be published. Required fields are marked *