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.
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:
For example, here, I have compressed a Debian ISO file:
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
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
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
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
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:
Now, here, I want to compress every file available inside the
Which can be done using the
gzip -r Debian
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
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
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
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
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
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
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?
I hope you will find the given examples helpful and if you have any doubts or suggestions, let me know in the comments.