Tar and Gzip utilities are often used together to create compressed TGZ/TAR.GZ files, which will be helpful in moving your data between systems efficiently. This article breaks down the differences between TAR, GZ, TAR.GZ, TGZ, and ZIP files. Keep reading to learn how to use these files in Linux.
What are the Differences Between TAR, GZ, TAR.GZ, TGZ, and ZIP Files?
What is a TAR File?
A TAR file's abbreviation stands for Tape Archive. A TAR file puts multiple files together. A TAR file does not reduce the size as there is no compression involved.
What is a GZ File?
A GZ file is created by the Gzip compression utility. A GZ file is a compressed single file, as the Gzip compression utility only compresses one file. If you compress multiple files with the Gzip compression utility, it produces multiple GZ files. The Gzip compression utility is common in Linux/Unix operating systems.
What is a TAR.GZ File?
A TAR.GZ file is a combination of a TAR file and a GZ file in that it is a TAR archive file that has been compressed with the Gzip compression utility. A TAR.GZ file most commonly found in Linux/Unix operating systems.
What is a TGZ File?
A TAR.GZ file is often called as and named as a TGZ file.
What is a ZIP File?
A ZIP file puts multiple files together just as a TAR file does, as well as having built-in compression just as a GZ file has. A ZIP file is most commonly found in Windows operating systems. The Zip compression utility is common in Windows operating systems.
What is a Tarball or Tarfile?
A tarball or tarfile is an informal name used name to refer to an archive file in one of the following TAR file formats:
- TAR File (Tape Archive file)
- TAR.GZ or TGZ File (only when compressed using the Gzip compression utility)
- TAR.BZ2 or TBZ File (only when compressed using the Bzip2 compression utility)
A tarball or tarfile is name of group of files that are bundled together. These files are produced by the command tar. Tar itself does not support compression directly. It is often used with a compression utility such as Gzip or Bzip2 to space disk space. These compression utilities generally only compress a single file, so they are used in combination with Tar, which can produce a single file from many files.
If the tarfile is compressed using Gzip compression utility, the tarball file will end with TAR.GZ. Using the Gunzip command, Linux users can uncompress TAR.GZ or TGZ files. Windows users can also create and expand tarball files using the 7-Zip compression utility.
Using Tar & Gzip/Gunzip to Create or Unzip GZ Files/TGZ Files in Linux
GZ and TGZ files are considered file compression and archiving standards for Linux systems. Therefore, knowing how to create and uncompress GZ and TAR.GZ files can help you accomplish different website administration tasks, including creating backups and restoring websites from them. In this article, we will learn what Gzip and Tar utilities are and how to create and unzip GZ and TGZ/TAR.GZ files in Linux.
Tar & Gzip: How They Work
Tar and Gzip are popular Linux system utilities for file compression and archiving. Although both tools are usually used together, Tar and Gzip perform different tasks.
The Tar File Archiving Utility
Tar is a utility used to merge several files into a single file, usually referred to as an archive, without compressing them. Another name for a tar archive is a tarball representing a file with the TAR file extension. You can think of a tarball as a container for multiple files that makes storing and moving them more convenient.
Tar does not change file system attributes such as permissions or ownership of the files included in a tarball; it saves them in an archive. Even after an archive has been created, you can still add or remove files from it or manipulate the filenames unless the archive has been compressed. The tar command is used to manage TAR and TAR.GZ files on a Linux system, including their creation, modification, and extracting files from them.
Tarballs were originally used to create backups and move them to local tape drives as tape archives, so that is where the term Tar (short for tape archive) got its name from originally. Unfortunately, Tar itself does not compress files, so the size of a Tar archive equals the amount of disk space the files it contains occupy. But nowadays, it is hard to imagine using Tar without file compression, which allows Tar archives to occupy less disk space and hence makes it easier for them to be transferred between systems.
Tar supports multiple compression methods, the most common of them are the Gzip/Gunzip and Bzip2/Bunzip2 compression utilities. Using Tar together with Gzip for file compression has become the best file archiving solution for Linux.
The Linux Gzip File Compression Method
Gzip is the most popular file compression utility for Linux systems which can be used alone to compress individual files. The Gzip and Gunzip commands compress a file, resulting in the new compressed version of it being created while the original file is removed by default. Files compressed with Gzip will have the GZ file extension. Therefore, when paired with Tar, the compressed archive will be saved as a TAR.GZ or TGZ file.
Gzip vs. Zip File Compression
Although Gzip uses the same compression algorithm as Zip which is a known utility for Windows, it can compress a single file at a time, so Tar is used first to create a tarball which Gzip will then compress. On the other hand, Zip compresses each file before adding them to an archive, which results in a bigger archive file size. However, having an archive compressed makes it more difficult to extract individual files without uncompressing the whole tarball first.
How to Create & Unzip GZ Files/TGZ Files in Linux
Tar and Gzip/Gunzip commands allow system administrators to create and unzip GZ and TGZ files. Like many other Linux utilities, they provide a set of flags that help you choose the options your need to use the functionality of the commands better to achieve specific goals. Gzip/Gunzip and Tar are installed by default on most Linux distributions, so all you need is SSH access and basic knowledge of the Linux command line interface.
Using Gzip/Gunzip to Create or Unzip GZ FIles
Gzip and Gunzip commands can be used to unzip GZ files in Linux, except for compressed Tar archives. Although a TAR.GZ file is a TAR archive compressed by Gzip, only the Tar command will allow you to uncompress and extract files from it.
How to Compress a File With Gzip
As we discussed, you can compress an individual file by creating a new version of it with the GZ file extension while keeping the same file permissions and ownership. By default, the original file will be removed, so you will need to unzip the GZ file to continue using it. However, this behavior can be changed. Below we will compress three files from the current working directory using Gzip compression.
# Compress multiple files with GZIP gzip -kv file1 file2 file3
We are keeping the original files using the -k flag and can see the percentage reduction and the names of the files processed as the command runs by using the -v option. Three new GZ files will be created in the directory due to the command above. The -k flag might be unavailable in some cases, so we will need to use the -c option to keep the original file.
We can use the -c flag to make the Gzip command usage more flexible. For example, we can use it to change the directory the new compressed file will be saved to and to change its name.
# Compress a file without removing it and place it in another directory gzip -c file1 > /home/temp/compressed_file1.gz
How to Open GZ Files Without Unzipping
You can use the zcat command in Linux to view the contents of a compressed file without unzipping it. It works for all compression utilities, including Gzip.
# View the contents of a GZIP compressed file zcat compressed_file1.gz
How to Unzip GZ Files
You can unzip GZ files in Linux by adding the -d flag to the Gzip/Gunzip command. All the same flags we used above can be applied. The GZ file will be removed by default after we uncompressed it unless we use the -k flag. Below we will unzip the GZ files we compressed in the same directory.
# Unzip GZ file gzip -dv compressed_file1.gz compressed_file1.gz: 52.8% -- replaced with compressed_file1
In the example below, the following commands are equivalents.
Using the gunzip command:
# Unzip GZ file gunzip file.gz
Using the gzip -d command:
# Unzip GZ file gzip -d file.gz
In the example below, use the following command to see the decompressed file.
Using the ls -1 command:
# List one file per line ls -1
Using Tar to Create and Unzip TAR GZ Files
The tar command is used to create and unzip the TGZ files on a Linux system. You can choose to unzip the whole archive or extract only the selcted files or directories. Below we will learn how to unzip TAR.GZ files from the command line using different flags.
How to Create a TAR.GZ Archive
Before creating a Tar archive compressed by Gzip, we need to understand what files we need to include and how we will group them together. Then, we can specify the files manually or create an archive of a whole directory and its subdirectories if there are any. Unlike using Gzip to compress individual files, creating a Tar archive compressed by Gzip will not lead to the removal of the files we include in it.
# Create a TGZ archive of a directory and save it to another folder tar -czvf tar_archive.tar.gz my_directory -C /home/temp # Place the chosen files in a TGZ archive tar -czvf tar_files_archive.tar.gz file1 file2 file3
Using the first command, we have created a Gzip-compressed Tar archive of the whole my_directory folder and placed it in the /home/temp directory. When using the second command, the tar_files_archive.tar.gz file will be saved to the current working directory and will only contain the three files we specified to be included.
We have used the following flags to specify the Tar command behavior:
- c - create an archive
- z - compress using Gzip
- v - verbose mode; you will be able to see a detailed output as the command is running
- f - specify the new archive filename
- C - specify a different target directory
How to View Archived Files
The -t flag allows you to view the continents of an existing TGZ archive file. In addition, you can use pipes to search for the files you need if the archive is large.
# List the contents of a TGZ archive tar -tvf tar_files_archive.tar.gz
How to Unzip TAR.GZ Files
You can Untar Gzip-compressed Tar archives using the -x (extract) flag the Tar command provides. Again, you can specify what directory you would like to extract the archived files to, as Tar will place all the contents of the TGZ file to the current working directory by default.
# Untar Tar Gz file and place uncompressed files in another directory tar -xzvf new_archive.tar.gz -C /home/temp
Frequently you will need to extract a specific file or folder from a TGZ file archive which is possible using the Tar command. Let’s take a look at the example below. We have our new_archive.tar.gz file and need to extract a file named myfile from it. We have confirmed it is included in the TAR.GZ file archive.
# Check whether the needed file is in the archive tar -tvf new_archive.tar.gz | grep myfile -rw-r--r-- root/root 108 2022-05-08 05:12 home/user/myfile
As the file is the home/user directory we archived, we cannot easily extract it without having to restore the whole directory. The strip-components option allows us to extract the desired files or directories without extracting their parent folders by specifying how many levels down the parent directory we need to descend. In our case, we are using 2 as the argument. Please note that you need to specify the full path to the file or directory you would like to extract.
# Extract myfile from the Tar Gz archive tar -xzvf new_archive.tar.gz new/new2/myfile --strip-components=2 new/new2/myfile
Now You Know All About File Compression Tools in Linux
In this article, we learned how to use Tar and Gzip for file compression and archiving and unzipping TAR.GZ files in Linux.
Tar and Gzip utilities are often used together to create TAR.GZ file archives while achieving a high level of compression. TGZ files are widely used in backup creation.
For example, hosting control panels like InterWorx and cPanel save local website backups as Gzip-compressed Tar file archives. Using Tar and Gzip can also help you move your data between systems more manageable and more efficiently by reducing its size and merging it into a single file.
Try Hosting With Nexcess
Every website needs a good host. As you know, choosing a reliable web host is crucial to a website's speed and security.
Get high-performance hosting from Nexcess. Why Nexcess? With our industry-leading visual comparison system and our award-winning 24/7/365 expert support, you can rest assured your website will work the way that it should.
Explore our fully managed our hosting solutions to get started today.