Linux Commands

How to Extract and Open a .GZ File in Linux Command Line

The GZ file extension is a zipped archive that follows the gzip compression method (GNU zip). The “.gz” format is designed to substitute the compressed formats on Unix-like operating systems. GZ compression is used to compress the webpages to reduce the page load time. GZ files can be opened and extracted using both built-in and third-party applications on any system. Here, we use the methods of extracting the “.gz” file on the Linux terminal. The prerequisite that is required to unzip the .gz files is that we just need the command line interface which is the Linux terminal and the graphical user interface access, as well as access to the “.gz” file itself.

Example 1: Create the .GZ File by Utilizing the Tar Command

Before extracting the .gz file from the command-line interface, we create it in our home directory.  The tar.gz file, also referred to as a tarball, includes multiple compressed files that are downloaded to conserve the storage space and bandwidth. For this, we use the tar command with the “-czvf” flag. Here, the “c” option indicates that the archive is created. The “z” option compresses the file using GZip, and “v” is the verbose mode which allows us to provide a comprehensive output while the command is executing. The “f” flag defines the new archive file. After that, we give the “MyDocuments” file name with an extension of “.tar.gz”. This file is created in the “/home/linux” destination.

$ tar -czvf MyDocuments.tar.gz /home/linux”

 
Now, we give the “ls” command which outputs all the directories and the files on the terminal. The compressed “MyDocuments.tar.gz” is shown in the following listed files and directories:

$ ls

 

Example 2: Extract the .GZ File Using the Gzip Command in Linux

The “.gz” extension symbolizes gzip which is a popular compression tool. First, we call the simple “gzip” command to create the file in the compressed format with the gz extension. The “gzip” command takes the “MyFile.txt” file for compression as displayed in the following:

$ gzip MyFile.txt

 
We check whether the “MyFile.txt” is compressed in the .gz extension by executing the “ls” command on the terminal.

$ ls

 

We create the compressed file in the Linux application with the gzip command. We can also use the “gzip” command to decompress it. The following command is used to extract the compressed “MyFile2” file. Note that we use the “-dk” flag along with the gzip command for decompression. The “d” option is used to decompress the “MyFile2.gz” and the “k” option keeps the compressed “MyFile.gz” file along with the extracted file.

$ gzip -dk MyFile2.gz

 
When we execute the previous command, it retains the compressed “MyFile2.gz” file along with the extracted “MyFile2” file. The extracted and the compressed files are listed on the command line interface.

$ ls

 

We have another convenient way of using the gzip command to decompress the file using the “.gz” extension. The following gzip command uses the “-decompress” flag along with the compressed “file1.gz” file. This command decompresses the .gz file into a specific directory.

$ gzip --decompress file1.gz

 
The “file1.gz” is now removed with the “file1” file name which is the extract form of the “file1.gz” file.

$ ls

 

The “uncompress” keyword can also be deployed to the gzip command to uncompress the “.gz” file. Here, we give the gzip command with the “uncompress” flag to the “file2.gz” file.

$ gzip --uncompress file2.gz

 
Execute the aforementioned uncompressed command which returns the file in the simple format which is seen in the following terminal. The “decompress” and “uncompress” flags which are used with the gzip command are simple for the user to comprehend.

Example 3: Extract the .GZ File by Utilizing the Tar Command

The tar command of Linux is an important utility that enables the archiving features. We can use the tar command to establish the uncompressed archive files, as well as to manage and update them. There, we have a tar command which is utilized to decompress the NewFile.tar.gz. We use the -xvzf option to extract the files from an archive where “x” is used to extract the archive, the “v” option displays the verbose data, “z” represents zip, and “f” generates an archive with the input “NewFile.tar.gz” filename. The given tar command determines the compression type and extracts the archive to the current working directory.

$ tar -xvzf NewFile.tar.gz

 
When the previous tar command is executed on the command line, the interface extracts the “NewFile.tar.gz” in the “NewFile” which is listed in the following image:

$ ls

 

Example 4: Extract the .GZ File Using the Graphical User Interface in Linux

If we don’t want to work at a terminal, thegraphical desktop environments provide all that you require. Just open the home directory or any other directory where the .gz file is placed. Here, we have a compressed file as “MyData.tar.gz” in the following image. To extract this file, we right-click on that file where the various options are shown. From these options, two options are used to extract the .gz file. The “Extract Here” option decompresses the file in the current directory while the “Extract to” option provides the destination directory for the extracted file to be placed.


We click the “Extract Here” option which decompresses the “MyData.tar.gz” file into the normal file within the same directory and removes the original one.


Next, on choosing the “Extract to” option, the directory of the Linux system is suggested in the following illustration to select the exact destination:


We select the “/Documents” directory where our “MyData” extract file is successfully placed as the following image. This way, we can extract any other compressed file, too.

Conclusion

This guide provides us with the basic concept of decompressing the .gz file. We run the various commands of Linux to extract the file with the “.gz” extension. This guide represents two distinct ways of unzipping the .gz files in Linux. The first one includes the command line interface where we use the gzip and tar commands with their different flags for the extraction of the “.gz” file. Then, we demonstrate the Linux GUI technique to unzip the “.gz” extension file.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.