Using gzip

In this tutorial we will be looking at the family of commands gzip, gunzip and zcat. These command will create a compressed archive in the zip format, unzip a compressed zip archive and display the contents of a file compressed in the gz format.

Important

The gzip command by default will zip a file and replace the original. If you don’t want the original replaced then further steps are needed.

How to use gzip to compress files

The basic syntax is gzip filename. This will compress the file and rename it filename.gz. The only option we will use in this tutorial is the -c option which sends the output to stdout or the screen. This will print an absolute mess to your screen so you will want to redirect this to a file. The -c option is used like this: gzip -c filename >filename.gz. This allows you to leave the original file unzipped and create a fresh zip file.

We recommend only using gzip directly if you want to zip up a single file. If you want to zip up multiple files please see our tar tutorial. The reason for this is that each file will be zipped individually whereas with tar all files are joined together and gzipped as one file. This greatly increases the efficiency of the gzip algorithm.

How to use gzunip to decompress archives

gunzip operates with the same syntax as gzip. The only option we will look at is -c. To extract a .gz file but keep the archive you would use gunzip -c filename.gz >filename.

Important

If you receive a file in .tar.gz please use the tar command to extract them.

How to use zcat

This program is useful for viewing rotated and compressed log files. The usage is simply zcat filename.gz. The output can be sent to less for paging. For example zcat /var/log/maillog.1.gz | less

Updated on April 4, 2018

Was this article helpful?

Related Articles