Skip to main content
Commands

How to Use the gzip Command in Linux

gzip is one of the most useful but often overlooked utilities. Learn to use this handy tool.

Sagar Sharma

If you want to share multiple files, compressing them all into one is the best solution you can opt for.

And on Linux, there is a utility named gzip intended to ease the whole process of compressing files and directories.

You can even manipulate the compression ratio for faster compressions!

In this guide, I will be sharing practical examples of how you can use the gzip command.

Use gzip command on Linux

To use the gzip command, you will have to follow the given syntax:

gzip [options] Target

In simple words, all you have to do is append the filename with a relative option (to change the default behavior) and it will get your job done.

Compress a file using the gzip command

To compress a file using the gzip command, all you have to do is append the file path of a file to the gzip command:

gzip filename

For example, here, I have compressed a Debian ISO file:

gzip Debian.iso
compress file using the gzip command on Linux

But if you notice carefully, it removes the original file after compression.

So what if you want to keep the original file? Well, here's how you do it.

Keep the original file while using the gzip command

So if you want to keep the original file while using the gzip command, you will have to use the -k flag with the gzip:

gzip -k filename
Keep the original file while using the gzip command on linux

And as you can see, it kept my original file!

Compress multiple files using the gzip command

If you want to compress multiple files, all you have to do is append the filenames with the gzip, and it will take care of the rest:

gzip file1 file2 file3

For your reference, here, I have compressed multiple ISO files:

gzip Debian.iso Fedora.iso linuxmint-21.iso
compress multiple files using the gzip command on linux

Check the content of the compressed file using the gzip command

So if you want to verify the contents of a compressed file without performing the decompression, you can do it using the -l flag:

gzip -l compressed_file

So let's say I have a compressed file named textfile.gz and I want to confirm the file contents, so I will be using the following command:

gzip -l textfile.gzip
check the contents of compressed file using the gzip command on linux

Compress files recursively using the gzip command

You might encounter multiple situations where you have to compress every file available in subdirectories, and in that case, you can benefit from this method.

First, let me share the file structure:

use the tree command to view file contents recursively

Now, here, I want to compress every file available inside the Debian directory.

Which can be done using the -r option:

gzip -r Debian
compress files recursively using the gzip command on linux

And as you can see, it compressed every file present in the current and sub-directory.

Decompress files using the gzip command

You can use the gzip command to decompress files when paired with the -d flag:

gzip -d compressed_file

For example, here, I want to decompress the Debian ISO file so, I will be using the following command:

gzip -dk Debian.iso.gz
decompress files using the gzip command on linux

Change compression and speed using the gzip command

The gzip command allows you to tweak the compression as per your preference.

So if you go for the best speed, you get the least amount of compression.

And if you are willing to spend some extra time, you can have the best compression possible.

Compress files faster using the gzip command

So if you don't care about the compression and want to get your job done as fast as possible, you can use the --fast flag:

gzip --fast file

Here, I will be compressing a Debian ISO file with the time command to know the time of execution:

time gzip --fast -k Debian.iso 
compress files faster using the gzip command on linux

And it took 1 minute, and 5 seconds to compress a file worth 3 GB!

Achieve the best compression using the gzip command

If you are ok with sacrificing some time to get the compression possible, it can be achieved using the --best flag:

gzip --best file

Here, I will be using the same Debian ISO and time command so that you can have a time gap between the best compression and the shortest time:

time gzip --best -k Debian.iso
get the best compression using the gzip command on Linux

And it took me 1 minute and 10 seconds to compress a file worth 3GB!

In conclusion, there was a gap of 5 seconds between the best compression and the fastest.

And this gap will get bigger if you are dealing with large files!

What about folders?

Did you notice that I only talked about compressing files with gzip, not folders? It is because to gzip a folder, you'll have to use the tar command.

How to gzip a Directory in Linux Command Line
Trying to gzip a directory and seeing an error in Linux? Here’s the command (with explanation) to gzip compress a folder in Linux terminal.

I hope you will find the given examples helpful and if you have any doubts or suggestions, let me know in the comments.  

Sagar Sharma