In this article, you will learn how to extract and compress TAR and TAR.GZ files, along with some common terms associated with compression and archives in Linux.

Basic Terminologies

Tarball: A tarball is a collection of multiple files in Linux stored as a single file. The term tarball comes from the coal-based sealant used during construction works. A tarball is often simply called a TAR file, which stands for Tape Archive. This is because the TAR filetype was originally created to store data in magnetic tapes. Gzip: GNU gzip is a file compression algorithm used to compress files. The file extension for gzip is GZ and therefore, you can deduce that any file ending with GZ has been compressed using the gzip algorithm. TAR. GZ: A TAR. GZ file is a version of a tarball compressed with the gzip algorithm. TAR is the file extension for tarballs, whereas GZ denotes gzip. The TGZ file extension is also used sometimes instead of TAR. GZ. Bzip2: Similar to gzip, several other file compression algorithms are also available, including bzip2. When you compress a TAR file using bzip2, the output file will have either of the following extensions: TAR. BZ2, TAR. BZ, or simply TBZ.

How to Create TAR and TAR.GZ Files

Creating archives is an important step when you’re backing up your Linux file system. This ensures that your backup remains unaffected and the files don’t corrupt if anything breaks on your system.

Using the tar Utility

The basic syntax to create compressed tarballs using the tar command is:

…where archive is the name of the compressed file and filename/directory is the file or directory you want to compress using tar.

The c, v, z, and f flags used in the aforementioned command stand for Create, Verbose, gzip, and Filename.

Note that you need to pass the file extension (TAR or TAR.GZ) in the archive name as follows:

To archive and compress the /Documents directory using tar:

You can also compress multiple directories and files by creating a single tarball. To do so:

Creating TAR and TAR.GZ Using 7-Zip

An alternative way of creating TAR and TAR.GZ archives is by using 7-Zip. The basic syntax of creating a TAR file with 7-Zip is:

…where a denotes Add an archive, -t denotes the Type of file, and tar stands for the TAR file type.

To add the /Downloads directory to an archive using 7-Zip:

7-Zip doesn’t allow the direct creation of TAR.GZ files. It’s a two-step process. First, create a TAR archive, then compress it into a TAR.GZ. If you already have a TAR file and want to compress it using 7-Zip, use the following command format:

The aforementioned command takes the archive.tar file as the input and zips it using the gzip algorithm. The output is the archive.tar.gz file.

You can combine both steps into a single command as well.

The -so and -si flags denote Standard Output and Standard Input. The first part of the command writes the archive.tar file to the standard output. The second command reads the archive.tar file from the standard input and compresses it accordingly.

How to Unzip or Extract TAR and TAR.GZ

Most of the time, you will get your hands on a compressed package that needs extracting. On Linux, there are multiple ways to unzip compressed archives.

Using the tar Utility

The basic syntax for extracting compressed files with tar is:

…where archive is the name of the compressed file. The collective -xvzf flag stands for Extract, Verbose, gzip, and Filename respectively. Anything that follows the -f option is treated as the input file. Note that if you are working with TAR files, you can remove the -z flag from the commands.

You can also unzip the content of the compressed file to a specific location as follows:

The aforementioned command will extract the archive.tar.gz file to the /Downloads folder.

To view the content of an archive prior to extracting it:

…where z, t, v, and f stand for gzip, List, Verbose, and Filename.

You can choose which files to extract from the archive. To do so, simply pass the file names with the default command.

Similarly, you can unzip specific directories from the archive as well.

Use the –exclude flag to specify the names of the files that you don’t want to extract.

Unzip TAR and TAR.GZ Files With 7-Zip

You can also extract a compressed archive using 7-Zip. The basic syntax is:

…where x stands for Extract.

For TAR.GZ files, you will have to unzip the compressed archive to TAR, and then further extract the TAR file using 7-Zip.

To extract a TAR.GZ archive directly using a single command:

Extract TAR and TAR.GZ Graphically

Most Linux distributions ship with a preinstalled archive manager. Decompressing TAR and TAR.GZ files is only a matter of few clicks using the GUI.

Locate the compressed archive file and right-click on it to bring up the menu.

Click on the Extract Here option to unzip the content of the file. The system will extract all the files to your current directory by default.

If you want to unzip the files to a different folder, click on the Extract To option. A file browser window will open. Choose the appropriate location and click Select to extract the file to that location.

Saving Storage and Bandwidth on Linux

When you want to share multiple files with someone else, compressing those files into a single TAR archive is the most efficient solution. Apart from occupying less space on your system storage, compressed archives utilize less server bandwidth when downloaded by multiple users.

A GZ file is nothing but a file compressed using the gzip algorithm. You can use the official GNU gzip utility to extract the content of the compressed archive file. If the file you’re looking at is a TAR.GZ, using either tar or 7-Zip to extract the file will be a much simpler choice.