The Linux operating system supports a large list of built-in command line utilities that can quickly access or manage the systems differently. The “gzip” and “gunzip” commands are built-in command line tools that facilitate the users for creating, displaying, or extracting the compressed files in Linux. The compressed files follow the various extensions such as “.gz”. “.bz2”, “xz”, and many others.
The guide elaborates on the possible methods to open or extract a “.gz” file. The outline of this post is listed below:
- Method 1: Using the “gzip” Linux Command
- Method 2: Using the “gunzip” Linux Command
- Method 3: Using the “tar” Linux Command
Let’s start with the “gzip” command.
Method 1: Using the “gzip” Linux Command
The “gzip” command is utilized for compressing the large file having the “.gz” extension. It provides a wide range of supported options that perform special tasks. In addition, the “gzip” command line tool is also beneficial for opening or extracting the compressed file with the help of its specific options.
The generalized syntax of the “gzip” command is written here:
$ gzip [option]... [file]...
The syntax contains the following components:
- gzip: Displays the “gzip” command.
- option: Shows the available options of the “gzip” command.
- file: Denotes the compressed target file.
Example 1: Decompress the GZ File (Normal Mode)
In this example, the “ls -l” command displays the “File1.txt.gz” located in the “sample” directory:
$ ls -l
Execute the “gzip” command with its “-d” option to decompress or open the “File1.txt.gz” file:
$ gzip -d File1.txt.gz
It is verified that the “File1.txt.gz” has opened successfully.
Example 2: Open (Unzip) the GZ File and Keep the Original
The “gzip” command does not keep the compressed file after uncompressing it. To keep saving both files, use its “-k” option. Here is the practical implementation of this concept.
$ ls -l
Type the “gzip” command with the combination of “-d” and “-k” flags in the following way:
$ gzip -d -k ZippedFile.txt.gz
The “ZippedFile.txt.gz” has been opened, and its compressed file is also available in the “Notes” directory.
Method 2: Using the “gunzip” Linux Command
The “gunzip” command is another command line tool utilized to decompress or open compressed files. The compressed files are recognized by the extensions “.gz”, “.z”, and many others. This method opens the file with the “.gz” extension format.
The generalized syntax of the “gunzip” command is written below:
$ gunzip [OPTION]... [FILE]...
The above syntax is divided into the below-mentioned parts:
- gunzip: Shows the “gunzip” command.
- option: Represents the supported options of the “gunzip” command.
- file name: Identifies the compressed target file.
Example: Decompress a GZ File
This example shows that there is a compressed file “Sample.deb.gz” in the “Downloads” directory as shown below:
$ ls -l
Use the “gunzip” command to open the compressed “Sample.deb.gz” file.
$ gunzip Sample.deb.gz
The output verifies that “Sample.deb.gz” has been opened or decompressed.
Method 3: Using the “tar” Linux Command
The “tar (tape archive)” command line tool is beneficial for the creation of archive files. The simple tar archive files follow the “.tar” extension, while the compressed tar archive files have a “tar.gz” extension. Once the tar archive is created, it can be opened by utilizing the “tar” command
The generalized syntax to open the compressed archive is defined
$ tar -zxvf [file_name.tar.gz]
The above syntax holds the following components:
- tar: Represents the “tar” utility.
- x: Helps to extract the archive.
- z: Beneficial to untar/extract the compressed archive file.
- v: Displays the verbose information of the archive on a terminal.
- f: Tells the archive with the given filename
- file_name.tar: Denotes the tar archive.
The directory “Extra” contains the “Test.tar.gz” compressed tar archive having the following contents:
$ tar -tf Test.tar.gz
Type the tar command in the terminal to open the “Test.tar.gz” archive file:
$ tar -zxvf Test.tar.gz
At this point use the “ls-l” command for the verification as shown below:
$ ls -l
It is verified that the “Test.tar.gz” is opened or displaying the “Extra” directory that is compressed in it.
It’s all about opening a “.gz” file in Linux.
Linux provides the “gzip” and “gunzip” commands to open the compressed file. This task can also be performed using the “tar” command line tool. These command line tools are available in the most commonly used Linux distributions. The guide has demonstrated all the possible ways to open the “.gz” file in Linux.