A GZ file is a compressed archive that uses the gzip compression algorithm (GNU zip). This format was created to replace compression formats on UNIX systems. It may contain multiple compressed files, directories, and stubs. This compression algorithm is more popular for compressed web pages to reduce page loading time.
The GNU zip compressed file uses .gz
or .z
extension. In this article, you will learn how to decompress (Unzip or extract) a GZ file via the command line.
Extracting GZ file:
We can use gunzip
command to decompress a .gz
file on Linux and macOS systems.
gunzip file_name.gz
A .gz
file is compressed with gzip
command line. That can also be used to decompress it with -d
parameter.
gzip -d file_name.gz
Both the above commands retrieve back the original file and remove the .gz
extension from file name.
Once you have decompressed a .gz
file, it will no longer avaialble. But, if you want to keep the archive file use -k
or --keep
option with unzip command.
gzip -d -k file_name.gz
## OR gunzip -k file_name.gz
Example
- First, use the following command to create gzip (.gz) archive of the access.log file. Keep remembering that the below command will remove the original file.
gzip access.log
- Above command will create a archive file named access.log.gz in current directory.
ls -l access.log.gz
-rw-r--r-- 1 root root 37 Sep 14 04:02 access.log.gz - Now use gunzip command to extract the access.log.gz file using the command. This will extract the file from the archive and remove the
.gz
file automatically.gunzip access.log.gz
Conclusion
In this blog post, you have learned about extracting a .gz
on Linux or macOS system using command line. You can also enable Gzip compression in Apache.
4 Comments
Great Script! thanks
Thank you. It’s clear and helpful.
Thanks, very helpful!
THANK YOU MAN YOU’R LIFE SAVER