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.