Linux Commands

How to unzip .tgz File Using the Terminal?

How to unzip .tgz file using the terminal copy

To transfer large files or to download large files to a device usually, the files are first compressed to save time and storage space. While using the Linux operating system, you will come across files that have the format of .tgz or tar.gz. This means that the file is compressed and to open up or to execute the file it is needed to be unzipped. The tar is the default utility that comes in the Linux operating system for compressing the files. There are a number of methods to unzip a file in Linux, and this guide is about unzipping a file on Linux using its terminal.

How to unzip .tgz File Using the Terminal?

The open-source packages for the Linux operating system are in the .tgz format, which compresses the file thus reducing the storage required by the file. There are primarily two methods to extract or unzip the .tgz compressed file in Linux which are:

Method 1: Unzip Using the tar Command

The tar utility is a pre-installed tool in the Linux operating system that is used to extract and unzip the compressed files having a format of .tgz. It is a simple and easy method, and below is the syntax fro using the tar command for extracting the .tgz file:

tar -xvzf <file-name>.tgz

Here, the flags used for the extraction of the file are:

  • The x flag extracts and unzips the compressed file.
  • The v flag creates the listing for the content in the file.
  • The z flag decompresses the compressed file, restoring it to its original size.
  • The f flag is used for specifying the name of the file.

For demonstration, a file with .tgz format is unzipped using the tar command following the syntax given above:

tar -xvzf test-file.tgz

Extracting the Zip File to a Specified Folder

To organize the data of the zip file if you want to extract the file to a different directory then just give the destination address as in the syntax below:

tar xf <file-name>.tgz -C <destination-address>

Here, the c flag is for copying the extracted contents of the file to the specified destination, below is the illustration for extracting the file contents to a specified folder:

tar xf test-file.tgz -C /home/linuxways/Files

Extracting Specific Directories and Files from .tgz File

If you want to extract only one or two files or directories in the.tgz file, then you can use the tar utility in Linux by stating their names along with the tar command. But before that, you need to list the contents of the compressed file, and for that use the below syntax:

To see the contents of the compressed file use the tf flag with the tar command along with the name of the compressed file as in the syntax below:

tar -tf <file-name>.tgz

Here the t flag is for the listing of the contents, for illustration I have listed the contents of the compressed file by executing:

tar -tf test-file.tgz

Now to extract the specific file below is the syntax for it:

tar -xvf <file-name>.tar.gz --wildcards*<File/directory-name>

For illustration, I have used the above syntax to extract a directory and a file from the compressed file:

sudo tar -xvf Testfolder.tar.gz --wildcards*File1”

Moreover, files can also be extracted using their extensions and in that case, we can use the above-given syntax:

sudo tar -xvf Testfolder.tar.gz --wildcards*.txt”

Method 2: Unzip using the gunzip Command

The gunzip is also a utility that can be used to compress and decompress the files in Linux and is pre-installed. To use this utility for decompressing the file first, the file is to be unzipped, and then it can be extracted using the tar command which makes it a two-step process:

Step 1: Unzip the .tgz File

As mentioned above, to extract the file using the gunzip utility first it is necessary to unzip it using the gunzip command, below is the command syntax for unzipping the .tgz file:

gunzip <name-of-file>.tgz

Now for illustration, I have used the above given gunzip command syntax and unzipped the .tgz file:

gunzip test-file.tgz

Step 2: Extract the .tgz File

Once the file is unzipped, extract its contents using the tar command, the syntax for using the command is:

tar -xf <name-of-file>.tar

Here the xf flags are used in the syntax where x is for the extraction and the f is for the file. For illustration, I have extracted the same file that was previously unzipped using the tar command:

tar -xf test-file.tar

Conclusion

The files having tgz format are usually the compressed files in Linux and are mainly used for open-source packages due to their large size. Such types of files are to be decompressed and unzipped to access their contents, and for that, there are primarily two ways. The first method for it is by using the tar utility, and the second method is by using the gunzip utility.

However, using the tar utility you can extract specific files and directories of the .tgz file and can also directly extract the file upon downloading by piping the tar command with the wget command. Another feature you can do by using the tar command is that the file can be extracted into a specific folder, which helps in organizing the content of the compressed file.

 

Similar Posts