How to Unzip .tgz File Using the Terminal?

Unzipping a “.tgz” file refers to the process of extracting the contents of a “.tgz” archive file. The “.tgz” file format is a type of compressed archive file commonly utilized on Linux systems. In Linux, the purpose of unzipping a “.tgz” file is to access the files and directories contained within the archive.

This guide will illustrate different methods to unzip the .tgz file using the terminal.

Note: The “office.tgz” file will be used in this post as an experiment which is located in the “Home” directory as shown below:

$ ls

The output shows that the “Office.tgz” file is located in the home directory.

Method 1: Use the tar Command to Unzip .tgz File 

The most common method for unzipping a “.tgz” file is possible through the “tar” command with the “xvzf” option. The syntax with practical implementation is explained below:

Syntax:

$ tar -xvzf zip.tgz

In the above syntax, the “zip.tgz” refers to the archive file, and “xvzf” is discussed below:

  • x: specifies extracting the contents of the archive.
  • v: displays the progress of the extraction process.
  • z: decompress the archive.
  • f: specifies the file name of the archive.

Example:

An example is carried here to extract the contents of an existing “Office.tgz” file using the “tar” command: The script is given below:

$ tar -xvzf Office.tgz

The output shows the extracted files “file.txt” and “file2.txt” in the terminal.

Note: For more details on the “tar” command, read our detailed article “tar command explained”.

Method 2: Use gunzip Command to Unzip .tgz File 

Using the “gunzip” command, extract the contents of the .tgz file by first decompressing it. After that, the “tar” command is utilized to extract the contents. The gunzip command only handles gzip-compressed files, not tar files. To extract a tar archive that has been compressed with gzip, use the “tar” command in combination with the “gunzip” command.

The syntax with practical implementation is explained below:

Syntax:

$ gunzip zip.tgz
$ tar -xf zip.tar

In the above syntax, “zip.tgz” refers to the archive file that needs to be decompressed. The “x” and “f” options represent the extracting of the contents of the archive with the file name.

Example:

For instance, the “Office.tgz” is carried out to decompress the file in the same directory. Then, the “tar” command with the “xf” option extracts the contents to present in the same directory: 

$ gunzip Office.tgz
$ tar -xf Office.tar

The output displays the content “file.txt” and “file2.txt” from the existing “Office.tgz” file.

Note: Read the dedicated article on “gunzip” command, i.e., gunzip command explained.

Conclusion

To unzip the “.tgz” file in the Linux terminal, utilize the “tar”, and “gunzip” commands. These commands required the name of the zip file to have the extension “.tgz” for extraction in the same directory. This article has explained all possible methods to unzip the file from the terminal.