Signup/Sign In

Gzip command in Linux

Gzip, short for GNU-zip, is simply used to compress and reduce the size of the file. This utility can be used with gzip command in almost every Linux distribution.

The compressed file has the same properties as the original file, like timestamp, and ownership. Files compressed by gzip command has “.gz” as the file extension.

gzip filename

It will create filename.gz and remove the original file.

In this tutorial, we will dive deep into gzip utility. Let's start with knowing more about the command and syntax.

Why you should use Gzip?

Gzip has a better compression algorithm due to which the archives generated from gzip has a smaller size than the ones generated through zip.

Gzip is different from zip because, unlike zip, gzip cannot compress multiple files in a single archive.

It can only compress a single file to an archive and if you have multiple files, then you either have to run the command for multiple files or you can bundle them using zip command in Linux and then compressing it with gzip.

Syntax

We can use this command to both compress and decompress the data. The general syntax of the command is:

gzip [options] [filenames]

Gzip command is generally used to compress regular files such as text and archives.

It's not recommended to compress media files such as video, audio, PDF, and other executable files because they are already compressed. And any further compression can corrupt the data.

gzip command options

There are multiple options/arguments available which can be used with gzip to increase productivity and save time.

Option Description
-c or --stdout Write output on standard output; keep original files unchanged.
-d or --decompress Decompress the compressed file.
-f or --force Force compression or decompression even if the file has multiple links or the corresponding file already exists, or if the compressed data is read from or written to a terminal.
-k or --keep Keep (don't delete) original input files during compression or decompression.
-l or --list List information about the compressed file such as compressed size, uncompressed size, ratio, uncompressed name.
-n or --no-name Do not save or restore the original name and time stamp.
-N or --name Save or restore the original name and time stamp.
-q or --quiet Suppress all warnings.
-r or --recursive Operate recursively on directories.
--rsyncable Make rsync-friendly archive that is more likely to compress similarly to other files with similar content.
-S or --suffix=SUF Use suffix SUF instead of .gz for the compressed file name.
--synchronous Synchronous output mode. This option can be used to avoid data loss due to system crashes.
-t or --test Test the compressed file integrity.
-v or --verbose Verbose mode. Display the name and percentage reduction for each file compressed or decompressed.
-V or --version Display the version number and compilation options, then exit.
-1 or --fast Compress faster but with less compression ratio.
-9 or --best Compress better but with slower compression speed.

Enough theory, it's time to get practical.

1. Compress Files using gzip command

Let's learn to compress a file using the gzip command in Linux. Use the following command with the file name.

gzip filename

It will create a filename.gz zip file in the same directory and delete the original file.

Compress files using gzip command

By default, gzip compresses the file and deletes the original file, keeping only the compressed file to save space.

Retain Original File while using gzip command

The above command deletes the original file and if we want to keep this file after the compression then use -k options in the command.

gzip -k filename

Compress files and keep input/original file

As you can see both original and compressed files are present.

Verbose Output to the console

Use -v option with the command if you want to see the detailed information related to the file compressions, and speed, etc.

gzip -v filename

gzip show compress stats with verbose option
In the above screenshot, we can see that main.go file is compressed by 71.8% and replaced with main.go.gz file.

2. Compress Multiple Files

To compress multiple files, use the following command. It requires the name of all the files with the command.

gzip filename1 filename2 filename3

The gzip command does not create a single compress file for all the files, rather it creates separate zip files for each file like: filename1.gz, filename2.gz, filename3.gz.

Compress multiple files with gzip

As you can see, all specified files are compressed.

Compress all files of a directory

If you want to compress all the files of a directory, then use -r option with the gzip command. The -r is used for recursive purposes.

gzip -r directory-name

It will compress all the files in the specified directory.

gzip command to compress all files in directory recursively

From above screenshot, all files in the temp directory got compressed.

3. Specify Compression Level

We can specify the compression level to the command to set the less or more compression quality.

Basically, it uses 1 to 9 numbers to set the compression level, which means the 1 will compress less and complete the process with fast speed while the 9 specify the best compression level but little slow speed.

We can set the level accordingly.

gzip -9 filename

It will do maximum compression.

Let's try compressing with fastest (-1) method:

Specify compression level in gzip command

Carefully note the compression percentage in the above image.

Now, let's try the same with more compression as the priority.Specify compression level for best compression with gzip command

From the output of both compression levels, we can easily spot the difference between both.

4. Decompress gz File

We can use the gzip command to decompress an archive file by using the -d option. The following command will decompress the file into the same directory.

gzip -d filename.gz

It will replace filename.gz with filename file.

Decompressing Multiple files

Similar to a single file, you can decompress multiple files as well by using the same command with multiple zip files.

gzip -d filename1.gz filename2.gz filename3.gz

Let's take a look example for both methods:

gzip command to decompress multiple archivesYou can use -r, -d and -v options together for recursive decompress all files in the folder and show stats.

FAQs

Let's take a look at some common queries related to gzip.

Q. How does gzip compression work?

Gzip compression works by finding common patterns in the data and replacing them with shorter symbols.

For example, if the word “Goodfellas” appears many times in a file, gzip can replace it with a single character.

Q. I haven't seen anybody using gzip. Is gzip used anywhere?

Almost every website uses gzip to serve static files. Enabling gzip compression on your website can improve your site’s performance and user experience.

Reducing the amount of data that needs to be transferred between the server and the browser, saving bandwidth and improving load times up to 60-70%.

Q. How to decompress files recursively with gzip?

You can use -r option with gunzip command (similar naming as bunzip command for bzip) as follows:

gunzip -r directory

This will traverse through the directory and its subdirectories and decompress all files in them.

Q. Can we specify a different suffix for compressed files with gzip?

You can use the -S or --suffix option for using a custom suffix or extension for compressed files.

gzip -S .zip filename

This will create a compressed file with a .zip extension instead of .gz. You can use any non-empty suffix, but it is recommended to use .z or .gz to avoid confusion.

This article contains a simplified guide to use the gzip command in Linux. Make sure to check out similar tool: bzip command, to choose a better tool for your purpose.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.