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.