Gzip which stands for GNU Zip is a file compression and decompression tool that you find in most of the Linux distributions. This tool will compress each file into a single file.

After the compression, the original file will be replaced by the compressed file which has a “.gz” extension as the suffix. For more information read the man page of gzip command by typing the following command in your Linux terminal.

man gzip
Man page of gzip command

Difference Between Gzip and ZIP in Linux

Similar to gzip, zip is also a file compression and decompression tool. When we compare gzip and zip, gzip is better in terms of compression. During a gzip compression, it archives all the files into a single tarball before the compression.

On the other hand, during zip compression, the individual files are compressed and then added to the archive. Therefore, when you want to extract a single file from a gzip compression, you need to decompress the whole files before extracting the file you want from the archive.

How Data Compression Works

The main reason that you need to compress your files is because of a condition called Data Redundancy. In simple terms, data redundancy is a condition that presents within databases in which the same piece of data is stored in multiple places. Assume you stored the following in a text file.


Now that text file has data redundancy because it contains similar data pieces in multiple places. For example, there are five “A” characters, four “B” characters and many more. So during the compression, what a compression program does is it keep track of how many places contain the same piece of data, and then it eliminates extra pieces of data by keeping only just one instance of each bit of data. For example, after the compression of the above text file, it would look like the following


It contains the same data as an uncompressed file but it’s simplified and uses less space. (numbers in front of the letters correspond to the number of times that the letter in front of them repeats.)

Also Read: How To Use grep Command In Linux With Regex

Using Gzip Command in Linux with Examples

The syntax for the Gzip command

gzip [Options] [filenames]


touch test 
gzip test
Using gzip command
Using gzip command

The above command will create a compressed file of test called test.gz and delete the original test.txt file.

Imagine you create another file with the same name “test” and try to gzip it. Then gzip will throw you an error saying that the file exists.

gzip error

If you need to compress the file ignoring this error forcefully, use “-f” as shown below.

gzip -f test

By default, gzip will delete the original file after the compression. But if you want to compress the file and keep the original file, use “-k” as shown below.

gzip -k test

Compress All Files in a Directory Using Gzip

Let’s create a directory named “samplefolder“. Inside the folder, I’m going to create three files as test1, test2, test3, test4, and another directory named “samplefolder2“. Inside the “samplefolder2” again I’m going to create three files as test1, test2, test3, test4

By using “-r” together with the gzip command it compress every file in a folder and its subfolders.

mkdir samplefolder
cd samplefolder
touch test{1..4}
mkdir samplefolder2
cd samplefolder2
touch test{1..4}
gzip -r samplefolder

When we use this option it doesn’t create one file called samplefolder.gz instead it will traverse the directory and compress each file.

Compress All Files in a Directory Using gzip

Also Read: Linux File Permissions Explained

Getting Verbose Output in Gzip

Using the “-v” flag we set the verbose mode which displays the name and percentage reduction for each file compressed or decompressed.

Getting Verbose Output in gzip

Decompressing Files with Gzip

To decompress a compressed file using gzip we can use “”-d” flag.

Decompressing files with gzip

Changing the Compression Level in Gzip

You can also set the compression speed and compression quality using “-#” where # equals 1-9. By default it will use “-6” . “-1” means faster compression (speed) but less compression (quality) while “-9” means slower compression (speed) but high compression (quality).

Changing the compression level in gzip

Using gzip you can reduce the file size by compressing the file. In this article, we discussed how to use gzip command in Linux. For more information, you can refer the manual of the gzip tool. I hope you enjoyed the article and learn something new. Thanks for reading.

Write A Comment