How to Unzip .gz File in Linux

Aashish Sunuwar Feb 02, 2024
  1. Use the gzip Command to Compress a File in Linux
  2. Use the gzip Command to Decompress a File in Linux
  3. Use the gunzip Command to Decompress a File in Linux
  4. Use the tar Command to Extract .tar.gz Files in Linux
How to Unzip .gz File in Linux

A GZ file is an archive file that has been compressed using the GNU zip (gzip) compression algorithm. It usually contains a single compressed file, but it can also hold multiple compressed files. The gzip command is primarily used for file compression on Unix operating systems.

Use the gzip Command to Compress a File in Linux

gzip filename

The command above compresses the file without keeping the original. We can use the following command to keep the original file as well.

gzip < filename > filename.gz

We can also maintain the original file by using the -k option.

gzip -k filename

Use the gzip Command to Decompress a File in Linux

We can use the -d option with gzip command to decompress the .gz compressed files.

gzip -d filename.gz

Same as in the compression, we can use the -k option to keep the original compressed file and decompress it.

gzip -d -k filename.gz

Use the gunzip Command to Decompress a File in Linux

We can also unzip the .gz file using gunzip command. To decompress using the file using gunzip, use the following command.

gunzip filename.gz

And to keep the compressed file.

gunzip -k filename.gz

Use the tar Command to Extract .tar.gz Files in Linux

The files with the suffix .tar.gz are just Gzip compressed .tar archives files. We can extract these kinds of files using the following command.

tar -xf filename.tar.gz

The archive will be extracted in the current working directory after the tar command automatically identifies the compression type.