Gzip is a well-known command or utility in Linux. It compresses and decompresses files or folders. Therefore, we are here to learn how to use gzip command in Linux. Besides, it is not that hard to learn.
It is a simple command with powerful compression algorithms. Gzip supports various compression formats like tar, gz, tgz, and more. It will be easy to compress or decompress a file once you learn this command.
Gzip was initially intended as freeware for UNIX-like systems. Its initial release date is 31 October 1992. Gzip uses LZ77 and Huffman coding algorithms for compression. Gzip is open-source software.
So Anyone can use it. It is now under the GNU license. It is a community-driven project. Therefore, it gets better day by day. Now after reading its history, it is time to get back to business. In this article, I will show how you can use the Gzip command in Linux. So let us get started.
Install Gzip On Linux
Gzip comes pre-installed in most Linux distributions. However, if you are unlucky and do not have it in your Linux distribution. Then use the following command to install gzip utility in Linux.
They are different ways to install gzip in different Linux distributions. Nevertheless, it comes down to two major Linux distributions. That is Red hat and Debian-based Linux distributions. Use the following commands:
- For Debian-based Linux distributions like Ubuntu, LUbuntu, and others.
apt-get install gzip
- For Red hat-based Linux distributions like fedora OS.
yum install gzip
You can use the above command to install Gzip utility on your Linux machine.
The article of your intrust: How to install Visual Studio Code in Linux
How To Use Gzip Command In Linux
Now I will explain how you can use gzip command in Linux. As I said earlier, it is not difficult once you get a hold of it. Keep in mind that gzip deletes the original file after the compression. You are left with the compressed file.
However, no need to worry. I will show you how you can do it without losing the original file. I will explain it further in the article. Just follow the tutorial below and you are good to go.
- First, open the terminal.
- Now install gzip if you don’t have it already.
- Go to the file using the cd command that you want to compress.
You might also like: How to Use the Chmod Command in Linux
Compress A file With Gzip Command
Use this command to compress a file using gzip. As I said earlier, it will delete the original file after compression. Only the compressed file remains.
- Use this command for simple compression
Compress the file without losing the original file
You can use this command to keep the original file
- For older version
gzip -c filename > filename.gz
- For newer versions of gzip
gzip -k filename
Compress multiple files with gzip in Linux
- You just need to put the name and extension of the file to compress them at once.
gzip file1 fil2 fil3 file4
Compress all files within the folder
You can use this command to compress the entire files within a folder. Gzip command will create a different compressed file for every file. All the sub-folders are compressed too.
- Type this command in terminal
gzip -r *
Compress a folder
- The tar compression format is used to compress a folder or directory.
tar czvf foldername.tar.gz / foldername /
In the command, you will see four characters “czvf”. Here is what they stand for.
- “c” means you want to create a new tar file.
- “z” means gzip will compress the folder
- “v” means verbose info.
- “f” means the file you create.
Above are the commands you can use to compress files. You will get the compressed file in .gz format.
Decide The Compression level Of Gzip command In Linux
The compression level determines the compression of the file. A low compression level means the compressed file takes less time to compress. But the file takes up more space in the storage. The high compression level is slow.
However, the compressed file takes less space on the disk. In Linux, the Gzip command provides three compression levels. First is fast but worst and denoted by 1. The second is moderate and denoted by 2.
The third is slow but best and denoted by 3. You can choose what compression level you want. Just put the number in the command like below.
- For fast compression
gzip -1 filename
- For moderate compression
gzip -6 filename
- For best compression
gzip -9 filename
Decompress files With Gzip Command In Linux
Decompressing is also easy with gzip. Just change the syntax and it is all the same as compressing. Like compression, gzip deletes the file you are working on.
However, this time around the compressed files is deleted, and decompressed remains. Use the following command to decompress a file.
gzip -d filename.gz
- you can also use this command
Decompress a file without losing the compressed .gz file
- It is just as we did in compression. Just use the –k attribute and you are good to go.
gunzip -c filename.gz > filename
Decompress all the files in a folder
- It is also the same as compression. just the attribute changes.
gzip -d *
See The Content Of A Compressed File Without Decompressing
In Linux, the gzip command allows you to see the content of the compressed file. You don’t need to decompress it. Use the following command.
That’s all about it. Now go ahead and try these commands yourself.
Gzip is excellent for file compression. We all face storage issues sometimes. Or we might want to add all the files in one place. We can do all of this by compressing files. Gzip is just the right tool you will need to do this job.
Hopefully, now you know how to use Gzip command in Linux without having a hassle. For more related articles read our what is Windows Subsystem for Linux [WSL] and you can also read this must have Apps for Ubuntu Linux and many more.