How to Unzip a .gz File Without Removing the Gzipped File?

In the world of file compression, “.gz” files are a staple, especially in the Linux and Unix environments. These files, known as “gzip” files are compressed using the “gzip” utility. It reduces the file size for quicker transfer over networks. 

However, a common task is to extract these “.gz” files without deleting the original zip file. This is particularly useful when retaining the original for backup purposes or when the extraction is part of a larger automated process where the “.gz” file is still required.

This guide will explain the unzipping of a .gz file while preserving the original one on Linux based on this content:

Unzipping a .gz File Without Removing the Gzipped File

Unzipping or extracting “.gz” files in Linux is a common task for users working with zipped files. 

Here, we will explore several methods to unzip a “.gz” file while keeping the gzipped file intact, using different commands available in Unix-like operating systems.

  • Method 1: Using the “gunzip” Command with the “-c” Option
  • Method 2: Using the “zcat” Command
  • Method 3: Using the “gzip” Command with “-d” and “-c” Options
  • Method 4: Using the “gzip” Command with the “-k” Option

Method 1: Using the “gunzip” Command with the “-c” Option

The “gunzip” command is the standard tool for decompressing “.gz” files. By default, “gunzip” will delete the original “.gz” file after decompression. 

However, users can utilize the “-c” option for writing the output to standard output. As well as, redirect the output to a file, thereby keeping the original “.gz” file:

gunzip -c itslinuxfoss.gz > file

The above command decompresses “itslinuxfoss.gz” into “file” while keeping the original “itslinuxfoss.gz” intact. 

Method 2: Using the “zcat” Command

The “zcat” is essentially the same as “gunzip -c”. It concatenates the uncompressed version of “.gz” files to standard output. With “zcat”, users can easily redirect the result to a new file as below:

zcat itslinuxfoss.gz > ILF

The above command decompresses “itslinuxfoss.gz” into a “ILF” while keeping the original one. 

Method 3: Using the “gzip” Command with “-d” and “-c” Options

The “gzip” command itself can be used to decompress files. The “-d” option is used to decompress, and similar to “gunzip -c”, the “-c” option will direct the decompressed content to standard output:

gzip -dc itslinuxfoss.gz > ILF-Raw

The above command decompresses “itslinuxfoss.gz” into a “ILF-Raw” without removing the gzipped file.

Method 4: Using the “gzip” Command with the “-k” Option

If users are required to keep the compressed file after extraction, they can utilize the “-k” option. For those who have “gzip” version 1.6 or later, the “-k” or “–keep” option is available. This option tells “gzip” to keep the input files during decompression: 

gzip -dk itslinuxfoss.gz

This command results in two files: the original “.gz” file and the decompressed version.

These methods provide flexibility in handling “.gz” files, ensuring that the original compressed file is not lost during the extraction process.

Bonus: Unzipping (Extract) Gz File in Linux

The “.gz” extension signifies that the file is compressed using the “gzip” compression algorithm, which is a standard method for file compression on Unix-like operating systems. To unzip (extract) a Gz file in Linux, follow the below methods:

Method 1: Using gzip Command

To unzip a “.gz” file, users can utilize the “gzip” utility, which is already installed on the Linux system. Let’s decompress a “itslinuxfoss.gz” file using the “gzip”:

gzip -d itslinuxfoss.gz

This command decompresses the file as well as removes the original one. 

Method 2: Using gunzip Command

Another common tool for this operation is “gunzip”, which is essentially an alias for “gzip -d”. To extract a file using “gunzip”, simply run the command by mentioning the .gz file:

gunzip itslinuxfoss.gz

Method 3: Using tar Command 

For “.tar.gz” files, that are tar archives compressed with gzip, users can utilize the “tar” with the “-xf” option. Let’s unzip the “itslinuxfoss.tar.gz” file:

tar -xf itslinuxfoss.tar.gz

This command extracts the contents of the “.tar.gz” file to the current working directory.

Important: It’s worth noting that while command-line methods are prevalent and efficient, desktop users who prefer a graphical interface can use their file manager to extract “.gz” files by choosing the file and hitting the “Extract” option.

Conclusion

To unzip a “.gz” file while preserving the original, utilize the “gunzip -c” command. It writes the output to standard output. You can then redirect this output to a new file using the “>” operator. For keeping the zip file after extraction, users can utilize the “-k” option. Alternatively, the “zcat” command, which is equivalent to “gunzip -c”, can also be used for this purpose.

To unzip a “.gz” file in Linux, use the “gzip” utility with the “-d” option, which stands for decompress. The command “gzip -d file.gz” decompresses the file as well as removes the original one.