File compression is a great way to save storage on a computer. If you're familiar with packages in Linux, you may have come across a TAR or a TAR.GZ file. While experienced users know what is a TAR file and how to extract it, Linux newbies might face some difficulties in figuring out what to do with it.

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:

        tar -cvzf archive filename
tar -cvzf archive directory

...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:

        tar -cvzf new.tar.gz big-file.txt
tar -cvf new.tar big-file.txt

To archive and compress the /Documents directory using tar:

        tar -cvzf new.tar.gz ~/Documents
    

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

        tar -cvzf new.tar.gz ~/Documents ~/Downloads file1.txt file2.txt
    

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:

        7z a -ttar archive.tar /folder
    

...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:

        7z a -ttar archive.tar /Downloads
    

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:

        7z a archive.tar.gz archive.tar
    

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.

        7z a -ttar -so archive.tar /Downloads | 7z a -si archive.tar.gz
    

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.

Related: 7-Zip Arrives on Linux: Here's How to Install It...

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:

        tar -xvzf archive.tar.gz
tar -xvf archive.tar

...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:

        tar -xvzf archive.tar.gz -C /Downloads
tar -xvf archive.tar -C /Downloads

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:

        tar -ztvf archive.tar.gz
tar -tvf archive.tar

...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.

        tar -xvzf archive.tar.gz file1 file2
    

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

        tar -xvzf archive.tar.gz directory1 directory2
    

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

        tar -xvzf archive.tar.gz --exclude=/Downloads --exclude=file1.txt
    

Unzip TAR and TAR.GZ Files With 7-Zip

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

        7z x archive.tar
    

...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.

        7z x archive.tar.gz
7z x archive.tar

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

        7z x -so archive.tar.gz | 7z x -si -ttar
    

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.

extract compressed archive files

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.

unzip files to other 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.