gunzip Command in Linux | Explained

The “gunzip” command is utilized to uncompress the compressed files. The “gunzip” command tries to handle both the GNU zip format of gzip and other Unix compress formats. The compressed files are recognized by the file extensions “.gz”, “.z,” and many others.

The aim of this guide is to explain the basics, working, and functionalities of the “gunzip” command in Linux. The outline of this guide is specified below:

Let’s get started with the basic working of the “gunzip” command.

What is the gunzip Command in Linux?

The working of the “gunzip” utility depends on the syntax provided below:

Syntax:

$ gunzip [Option] [file name]

The above syntax contains the following components:

  • gunzip: It is the main keyword in the syntax.
  • option: These are the supported options that the “gunzip” command offers to get the desired output.
  • file name: The target file must be decompressed or unzipped.

The “gunzip” command offers many options that are illustrated below, along with their description: in the following command’s output.

$ gunzip --help

Let’s dig into the usage of the gunzip command.

How to Use gunzip Command in Linux?

This section comprises various examples to explain the working “gunzip” command by utilizing these options.

Example 1: Unzip the Compressed File

The “gunzip” command, without any argument, simply unzips or decompresses the gzip/ compressed file. For example, there is a compressed file with the name “file1.txt.gz” as shown below:

$ ls -l

Use the below “gunzip” command in the console for decompressing the compressed “file1.txt.gz”:

$ gunzip file1.txt.gz

The output confirms that “file1.txt.gz” has been decompressed.

Unzip Multiple Compressed Files

The user can easily decompress more than one file at a time through the “gunzip” command. The general syntax for decompressing multiple files is written below:

$ gunzip [file1].......[file N]

Use this syntax to uncompress the compressed files “File1.txt”, “File2.txt”, and “File3.txt” at a time:

$ gunzip File1.txt File2.txt File3.txt

All the mentioned files are unzipped easily.

Example 2: Display the Compressed File Content

The “-c” argument allows the users to read the content of the compressed file. It only shows the compressed file content without decompressing. In this example, the compressed file “File4.txt” contains some information about “Linux”. Run the following command to show it without unzipping the file:

$ gunzip -c File4.txt

The output verifies that “File4.txt” is still compressed, and the “-c” flag only displays the file content.

Example 3: Extract the Compressed Files Recursively

The “gunzip” command offers the “-r” option for unzipping the compressed files and subdirectories available in a specific directory or subdirectories. As the “ExtraDir” directory contains “three” zipped files as shown in the screenshot:

$ ls -l

Now, unzip the above-zipped file recursively by typing the following command:

$ gunzip -r ExtraDir

All the zipped files in the “ExtraDir” directory have been unzipped now. Now the output displays only the unzipped files, not the zipped files.

Example 4: Display the Zipped File Information

The “-l” option displays the compressed or zipped file information in the terminal. In this example, the zipped file “File4.txt” information is displayed by issuing the below-mentioned command in the console:

$ gunzip -l File4.txt.gz

The output shows the “File4.txt” “compressed”, “uncompressed”, “ratio”, and “uncompressed_name” columns. Also, it displays that after compression, the “File4.txt” has a size “233” bytes, and before compression, the file size was “317” bytes.

Example 5: Save Compressed and Uncompressed Files

The “gunzip” command does not keep the compressed file after uncompressing it. To keep saving both files, the “gunzip” command offers the “-k” flag. It keeps both files in their original place or path. In this example, there is a compressed file in the “Dir” directory:

$ ls

Type the “gunzip” command with the combination of “-k” and the target file “NewFile1.txt” and see the results:

$ gunzip -k NewFile1.txt

Here, both the zipped and unzipped files are in their places.

Conclusion

In Linux, the “gunzip” command line utility is used to unzip the zipped files. It is part of the GNU package utility and is available in almost all Linux distributions. Furthermore, it facilitates the users with its various options. This guide has briefly demonstrated the objective, working, and usage of the “gunzip” command in Linux.