Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Gzip command in Linux

Gzip command is simply used to compress and reduce the size of the file. 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

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


gzip [arguments] [filename]

Gzip is different from zip because, unlike zip, gzip cannot compress multiple files at once. It can only compress a single file at a time and if you have multiple files, then you either have to run the command for multiple files or you can bundle them using tar or zip command in Linux and then compressing it with gzip.

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.

Advantages of gzip over zip

  1. Gzip has a better compression algorithm due to which the archives generated from gzip has a smaller size than the ones generated through zip.
  2. It is almost 10 times better when compressing multiple files bundled together because instead of compressing multiple files, it will take them as a single file and reduce the file size by a huge margin.

Disadvantages of gzip

  1. Unlike zip, if you want to extract a single file from the archive, you have to decompress the whole archive and then extract the file. Whereas in zip, you can simply extract and decompress a specific file.
  2. Compressing and decompressing multiples files requires multiple steps and is time-consuming. You have to bundle the files together using tar or zip and then compress them using gzip. The same goes for decompression, you have to decompress it and extract the files using tar or zip.

Options/arguments available for gzip

There are multiple options/arguments available which can be used with gzip to increase productivity and save time. These are optional and you can compress and file using the default settings without typing any arguments.

-f (Forced compression): This argument is used when there's already a compressed file with the same name. For eg., if you are trying to compress a file named cars.txt and there's already a compressed file with the name cars.txt.gz, using -f will force gz to compress the file and save it.

-k (Compress and keep): This argument will tell gzip to compress the file and save both the original file and the compressed file. By default, gzip compresses the file and deletes the original file, keeping only the compressed file to save space.

-v (Stats display): This argument will show how much the file's size has changed after compression or decompression after gzip has finished running.

-l (Progress display): This argument shows the progress percentage during compression or decompression.
-d (Decompression): This argument is used along with gzip to decompress the file. You can also use this argument with the above-mentioned arguments.

Compress File 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 .gz zip file in the same directory and delete the original file.

Retain Original File while using gzip command

The above compress 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

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

filename: 32% -- replaced with filename.gz

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 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 same directory.

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.

Decompress File in Linux

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

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


The gzip command is a utility command in Linux that can be used to compress and decompress files. In this article, we learned to use it with several options such as -k to retail the original file, -v to get verbose information, -d to decompress the .gz file, etc. This article contains a simplified guide to use the gzip command in Linux.