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

While extracting files in Linux, the original compressed file may be updated with the extracted content which does not allow the user to reuse the zipped/gzipped file. However, the users have the choice to keep the zipped/gzipped folder safe after extracting it to make it reusable. 

Considering its importance, this article will explain different methods to unzip the .gz file without removing the gzipped file:

Method 1: Using the “gunzip” Command

To access the contents of the gzipped file, the “gunzip” command unzips the file and returns the original data. The practical implementation is provided below:

Syntax:

The syntax of the “gunzip” command is as follows:

$ gunzip -c zip_file.gz > unzip_file

In the above syntax, the “zip_file.gz” refers to the gzipped file that needs extraction, while, “unzip_file” is the extracted file. The “-c” option refers to “copy to standard output”.

Example:

For instance, the “my_file.gz” is extracted to a file named “final_file” without removing the gzipped file using the “c” option. It copies the contents of the .gz file and sent to the file “final_file”:

$ gunzip -c my_file.gz > final_file

The output shows that unzipping a “my_file.gz” to “final_file” without removing the gzipped file.

Method 2: Using the “zcat” Command

The “zcat” command is similar to “gunzip -c” and provides another way to extract the contents of a .gz file and send it to the specified file. 

Syntax:

The syntax along with a description of the “zcat” command is provided here:

$ zcat zip_file.gz > unzip_file

The “zcat” command unzips the gzipped file “zip_file.gz” and extracts the content in the “unzip_file”.

Example:

In our case, unzip the content of the gzipped file “my_file.gz” to the extracted file named “new_file”:

$ zcat my_file.gz > new_file

The output shows that “new_file” has been created without removal of the “my_file.gz” file.

Method 3: Using the “gzip” Command

The “gzip” command decompresses the contents of the gzipped file and sends it to the specified location in the extracted format.

Syntax:

The syntax of the “gzip” script is given below:

$ gzip -dc zip_file.gz > unzip_file

In the above syntax, the “zip_file.gz” represents the gzipped file, and the “unzip_file” is the extracted file. The “-d” option stands for “decompress“, and the “-c” option means “copy to standard output“. 

Example:

An example is carried out to unzip the gzipped file “my_file.gz” to the extracted file named as“office” in the below script:

$ gzip -dc my_file.gz > office

The above display authenticates that “office” has been created without removing the gzipped file.

Conclusion

To unzip a .gz file without removing the original gzipped file, users can utilize the “gunzip”, “zcat” and “gzip” commands. After extraction, users can access the original data of the zipped file without modification. This guide has illustrated different methods to unzip a .gz file in Linux without removing the original gzipped file.