Using Tar & Gzip/Gunzip to Create or Unzip GZ Files/TGZ Files in Linux

    Knowing how to create and decompress GZ and TGZ/TAR.GZ files can help you accomplish different website administration tasks, including creating backups and restoring from them

     Using Tar & Gzip/Gunzip to Create or Unzip GZ Files/TGZ Files in Linux

    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.

    Overview of File Compression Terms & Tools in Linux

    What are the Differences Between TAR, GZ, TAR.GZ, TGZ, and ZIP Files?

    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. 

    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. 

    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.

    TGZ File

    - A TAR.GZ file is often called as and named as a TGZ file.

    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 Nexcess Knowledge Base 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
    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:

    • - create an archive

    • z - compress using Gzip

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

    Conclusion: File Compression Tools 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. In this article, we learned how to use Tar and Gzip for file compression and archiving and unzipping TAR.GZ files in Linux.

    Consider Managed Hosting with Nexcess

    Nexcess provides the needed level of automation to all routine website administration tasks that require technical knowledge and skills. Our Nexcess Client Portal is specifically designed to help website owners manage all technical aspects of their hosting plans leveraging a highly intuitive and user-friendly interface.

    Our fully managed hosting plans are optimized for the chosen platform to provide you with the best approach to website hosting. Enhanced security, better performance, high flexibility, and great scaling options for a reasonable price are just some of the benefits a website owner gets by choosing one of the Nexcess Managed Hosting Plans.

    Managed Applications with Nexcess

    Hosting optimized for the industry's leading platforms including Managed WordPress HostingManaged WooCommerce Hosting, and Managed Magento Hosting

    Build Better Sites and Stores With Fully Managed Hosting from Nexcess 

    Faster Speeds, Stronger Security, Inherent Scalability and Trusted Support.

    Our range of hosting plans caters to any business scale. We do all the heavy lifting for you to focus on growing your business. In addition, we automatically update ore components and plugins to the latest version and ensure your hosting environment is properly optimized, secured, and updated.

    Nexcess infrastructure was specially designed to keep up the best speed and performance in the industry. No traffic surge can threaten your website to go down thanks to our autoscaling technology which adds more resources to your server to handle the load. In addition, we offer always-on security monitoring and support from web hosting experts 24/7/365 and a built-in CDN with 22 locations and advanced caching for ultra-fast loading.

    All hosting plans include The Nexcess 30-Day Web Hosting Money Back Guarantee.

    We also have a variety of Nexcess support articles about how best to implement our product offerings housed in the Nexcess Knowledge Baseincluding how to get your site going with a number of different configuration options. These resources include a great article on setting this up for Migrating to Nexcess with managed WordPress and managed WooCommerce hosting

    Need a No-Code Website Building Solution? 

    Are you on an accelerated schedule and already ready to move forward? If you need to get started with your Nexcess StoreBuilder Solution sooner rather than later, check out these resources:

    24-Hour Assistance

    If you need any assistance with the above-mentioned, don't hesitate to reach out. For 24-hour assistance any day of the year, Nexcess customers can contact our support team by email or through your Client Portal.

    Why Choose Nexcess?

    Because we're different! Chris Lema captures "the why" in his passionate and stirring recount of a Nexcess support-related story.

    Useful YouTube > Nexcess Channel Links

    Resources for More Information

    Need more help? The Website Management .htaccessand Scripts, Cron Jobs, Reports and Monitoring, IP Management, and Dev Sites sections within the Nexcess Knowledge Base are important resources for those seeking additional knowledge. The Applications section also contains valuable insights for those seeking additional knowledge about our other various hosted applications and platforms. Check out our related video playlists and articles below:

    New Customers: Fully Managed Hosting Solutions

    Not a Nexcess customer yet? Check out our fully managed hosting solutions. The option to chat with an expert is also available.

    Related Articles

    Was this article helpful?

    Send feedback

    Can’t find what you’re looking for?

    Our award-winning customer care team is here for you.

    Contact Support