Home / Linux/Unix/BSD / Untar tar.gz

Untar tar.gz

A tar file which is also referred to as a tarball, is a collection of files that provides easier storage. We can use the tar command to create archives along with extract files, list files, storage purposes, or update purposes.

Using a tar.gz file is useful because it provides a better storage solution rather than constantly keep track of multiple files.

In this tutorial, we’ll guide you through how to extract a tar.gz file, list tar.gz files, and extract specific files from the tar.gz file.

What is TAR?

Tar is short for Tape Archive, and commonly referred to as a tarball (a collection of files).

Using the tar format has grown in popularity over the years thanks to its archiving/storage purposes and allows users to send multiple files over the Web/Internet.

Tar is common in Linux systems and is used only for storing data.

If you want to compress files on your tar, this will require an extra step performed manually and become tgz files through the gz, tgz, or tar.gz extension.

How to Extract Tar.gz File

A tar.gz is essentially an archive file that acts as a storage container for files. Most Linux distributions will also come pre-installed with the tar command.

To extract a tar.gz file, we use the x option (extract) and then specify the archive file you want to extract after the f option. The command is as follows:

tar -xf archive.tar.gz

This tar command will also work for tar archives compressed through other algorithms like tar.bz2.

While the tar command is primarily used for the command line, you can also unzip tar.gz file using the File Manager.

To do so, simply right-click on the tar.gz file you want to extract and click on “Extract.” Take note that for Windows users, you will need 7zip to unzip tar gz files.

If you use the -v option, this will allow the tar command to be more visible and also print the names of the tar files you will be extracting. Simply type the tar -xvf command followed by the tar.gz file:

tar -xvf archive.tar.gz

When you extract tar.gz files, this will automatically extract files in your current directory.

If you don’t want to extract tar.gz files in your current directory, you can use the -C option to extract the it into a specific directory.

For example, you want to extract a tar contents into the directory /my_drive/Linux/files, use the command:

tar -xf archive.tar.gz -C /my_drive/Linux/files

How to Extract Gzip Compressed Tar Files

Now, what happens when you want to extract the archive contents of a tar file but are compressed with a gzip compressor? First, you’ll have to uncompress it using this command:

tar xvzf archive.tar.gz

x – This tells tar to extract the file or files

v – Lists all the files separately in your archive

z – Informs the tar command to uncompress the gzip file

f – Tells tar you are giving it a file name to work with

Similarly, you can use the -C option to uncompress the tar gz file onto a separate directory using the command:

tar xvzf -C archive.tar.gz /path/to/my_drive/Linux/files

Finally, you can also extract a file without using tar on your command line:

gunzip sample_file.gz

How to List a Tar.gz File

Let’s say you want to extract tar.gz files, but only specific ones in your archive. To ensure you don’t accidentally delete any tar archives, you’ll need to learn how to list all the files found on your tar first.

There are two ways to list the files on your tar, wherein one will use the -v option as part of the command.

Option 1:

To list all the files, we’ll use the -t option:

tar -tf archive.tar.gz

You’ll be prompted with a display on your command line similar to this:

file 1
file 2
file 3
file 4

Option 2:

If you want to list all the files on your tar archives and want to see more information such as file size, timestamp, and the owner, we use the -v option:

tar -tvf archive.tar.gz

How to Extract Specific Files From Tar.gz

You’ve learned how to list all the files in your tar, so when extracting specific files on your command line, we use the tar -xf command followed by the names of the files separated by a space for each file.

For example, you want to extract sample_file1 and sample_file2, using the tar -xf command:

tar -xf archive.tar.gz sample_file1 sample_file2

The tar -xf command also works if you want to extract a specific directory:

tar -xf archive.tar.gz directory 1 directory 2 directory 3

If you want to extract .jpg (jpeg) or .js (javascript) files for example, you can type the tar -xf command followed by the –wildcards option:

tar -xf archive.tar.gz --wildcards '*.jpg'  

If you attempt to extract a file that is non-existent in the system, you’ll be prompted with an error message:

tar -xf archive.tar.gz SAMPLEREAD
tar: SAMPLEREAD: Not found in archive
tar: Exit with failure status due to previous errors

How to Extract a .Tar.bz2 file

You’ve learned how to untar a tar.gz file along with how to extract specific files, gzip compressed files, tar contents into a specific directory. What happens when it’s compressed with a bZip2 compressor?

This process will also follow the same extraction process as the Gzip compressor, only that instead of using the -z option, we’ll use the -j option:

tar -xvjf archive.tar.bz2

The -j option will inform tar to decompress the bZip2.

How to Create a Compressed Tar File

There are two options you can do this: use the program 7-zip or through the command line.

Option 1: Using 7-zip

Step 1: Open the 7-zip program

Step 2: Select all folders and files you want to place in the tar file

Step 3: Right-click on any one of the highlighted items and click “Add to Archive.”

Step 4: From the Archive format drop-down menu, select “tar” and press “OK.”

Option 2: Using the command line

Let’s say you want to tar a file from a folder named “samplefiles” into files.tar.gz. The syntax will be as follows:

tar -czvf name_of_archive.tar.gz /path/to/folder or file

Using the example “samplefiles” the command will be as follows:

tar -czvf files.tar.gz /usr/local/samplefiles

Final Thoughts

We hope this tutorial was able to help you understand how to extract tar.gz files! If you have any more questions, let us know in the comments section below!