The gzip Command in Linux

Gzip Command In Linux

Working with archives in Linux is easy. There are a few commands that take care of almost all the archive types for you. The gzip command in Linux is one such command. Without any arguments, the gzip command will automatically compress the files that you specify right after the command.

How to Compress Files With the gzip Command in Linux?

It’s as simple as typing gzip followed by the filenames. Have a look at the syntax below:

gzip <filenames separated by spaces>
Gzip command in Linux Compress
Gzip Compress

Notice how the “helloWorld” file went from 2MB to 1012KB after we compressed it using gzip. But there’s one problem. The gzip command doesn’t understand if you want to compress multiple files.

In such a case, you need to compress those multiple files in a tar format and then compress that to gz. That’s why we have tar.gz files.

Check Compressed File Statistics

To check what the compressed and uncompressed file sizes would be, you can use the -l command option.

root@ubuntu:~# gzip -l helloWorld.gz 
         compressed        uncompressed  ratio uncompressed_name
            1035475             2008801  48.5% helloWorld

The sizes and the compression ratio is displayed right here. You can display the same data for all the gzip files in the directory with the use of the asterisk wildcard.

Keep The Original File After Compression

As you saw above, the original file was lost after the compression. But if you want to keep the original file even after the compression is complete, you can use the -k option.

gzip -k <filename>
Gzip Keep Original Files
Gzip Keep Original Files

Setting Compression Levels Using the gzip Command in Linux

By default, gzip command in Linux performs a level 6 compression but allows us to specify the compression levels based on speed or compression quality.

The compression levels range from 1 to 9 where level 1 offers the fastest compression speeds whereas level 9 offers the highest compression ration.

Let’s find out if there’s any difference when we compress our helloWorld file with level 9 compression.

gzip -9 <filename>
Gzip Level 9 Compression
Gzip Level 9 Compression

Since the file is already pretty small, the compression difference is just as small. By default, the compression resulted in a 1012K file. With the best compression, it’s now 1007K. This difference is very noticeable when you’re working with large files.

Now that we know how to compress files, let’s learn to decompress files using the gzip command in Linux.

Decompress/Uncompress a Gzip File

Once you’ve compressed a file, you cannot just rename the file and remove the .gz extension to retrieve the original file. We need to decompress the file. To decompress the file, we can use the -d option.

gzip -d <filename>.gz
Gzip Decompress
Gzip Decompress

Retain the Compressed Files After Decompression

Alright, now similar to the problem with compression, decompressing a file deletes the original compressed version of the file that we pass as an input. How do we solve that? We use the -k option again.

gzip -dk <filename>.gz

This will keep the compressed file intact when decompressing a file with the gzip command in Linux.

Compress and Decompress Entire Directories

We have been working with individual files up until now. When you want to save the disk size of entire directories, you need to use the -r option which stands for recursive. A common option among many Linux commands to denote that we’re working with directories.

gzip -r <directory>   # Compress

gzip -dr <directory>   # Decompress

This will compress or decompress all the files within a directory as shown below:

Gzip Compress Directory
Gzip Compress Directory

In the above screenshot, I’ve compressed and decompressed the entire directory. As you can see, the two files in the directory were compressed to a smaller size and then decompressed later.


The gzip command in Linux is pretty small and there’s not too much to cover in it. If you’re interested in learning more about the command, you can read the manual page using the man command.

Leave a Comment

Your email address will not be published. Required fields are marked *