If you are using open-source packages, then sooner or later, you will encounter the .tgz or .tar.gz files. This is a variation of zipped files; most open-source packages use this format to zip their files. If you are new to Linux or recently encountered the problem of unzipping the .tgz file, then you don’t have to worry this guide will teach you ways of unzipping the file.
The tar is a default utility that comes with your Linux, and files compressed using the tar utility are tagged with .tgz or .tar.gz. Tar creates an archive for your files by combining multiple files. This is a widely used format as it saves a lot of space and bandwidth while installing downloading and installing the packages.
This article will cover two methods of unzipping the .tgz files using the terminal. Let’s get into these.
Note: For this particular tutorial, we will use the Ubuntu 22.04. However, the commands used here are same for all the distributions.
Approach 1: Unzip Using the .tar Utility
The most common method of decompressing a file on Linux is the .tar utility. As it covers a wide range of compression and decompression mechanisms such as bzip2, Izip, Izma, gzip, compress, xz, and Izop. here we will be using the .tar utility to Unzip the files.
The command for unzipping the .tgz file using the .tar utility:
tar -xvzf <File name>.tgz
In this command -xvzf flags are used, and these flags pass commands to the .tar utility.
- x – Flag is used to extract the zipped files
- v – Flag is for verbose, or creating a list of extracted files
- z – Flag stands for decompression of the files
- f – Flag gives the .tar utility the file name for working
Example 1: Unzipping files
In this example, the .tgz file of Softmaker-free office-2021 is used. let’s first check the .tgz file location and get its file name. The file name is necessary and will be used in Unzipping the file. Use this command to get the list of files in the current directory:
As we can see, the file is in the home directory; from here, you can move on to the next step and unzip the file using this command along with the file name. If you don’t know the exact file name, you can copy it from your ls output and paste it into your command. In this example, the file name was copied and added to the command:
tar -xvzf softmaker-freeoffice-2021-1062-amd64.tgz
By running the above command, you have unzipped the files from the .tgz zipped file. The information on unzipped files is shown in the output results of the command.
Example 2: Unzip Files in a Specific Directory
If you are also looking for a method to save the unzipped files in a special location of your choice, then use the following command:
tar -xvzf <File name>.tgz -C /home/user/DestinationFolder
This command contains the .tar function with -xvzf options for list, decompression, and extraction of the files. After the file name and its extension -C is used for the directory name with a path for the destination folder. In this example, the following command will be used with the file name and folder location:
tar -xvzf softmaker-freeoffice-2021-1062-amd64.tgz -C /home/linux-user/Downloads/
With this; you have now extracted the files, got a list of files in a .tgz folder, and learned how to unzip files in a location of your choice.
Example 3: List the Files Without Unzipping
If you want to list the files inside a .tgz without extracting the file you can use the following command:
tar -tzf <File name>.tgz tar -xvzf softmaker-freeoffice-2021-1062-amd64.tgz
By removing one of the flags responsible for the extraction of the file, you will get a list of files present inside the .tgz file.
Approach 2: Unzip Using the gunzip Command
Gunzip is another installed tool by default in Linux distributions. For this, you have to first unzip the files using the gunzip and then those unzipped files are extracted using the tar utility. So, it is a two-step process. Let’s learn how it works:
Step 1: Unzip the Files
The command for unzipping the .tgz file using gunzip:
gunzip <File name>.tgz
This command has the gunzip function with the file name and .tgz extension as practiced in the following command.
Step 2: Extract the Files
After unzipping the files from the zipped folder, use this command to extract the files from the .tar file:
tar -xf <File name>.tar
This command content tar utility with -xf flags to extract the files and save the files with folder names. The command, for example, will look like this:
tar -xf softmaker-freeoffice-2021-1062-amd64.tar
With this, you have unzipped the contents from the zip file.
Most open-source applications use the .tgz format to zip their files, and in this post, we have learned two different methods of unzipping these files. First, we learned to unzip using the .tar utility, a default utility that supports many formats and ways of unzipping files. In the second solution, you have the gunzip utility, a default utility of Linux distribution.
Keep visiting Linux-Genie for more Linux tips and tricks.