Compression is a very commonly performed operation by users to save disk space as well as reduce time and bandwidth while transferring large amounts of data over the network using gzip utility.
gzip stands for the GNU zip and it is a very popular compression and decompression utility. One of the primary reasons for its popularity is its high compression ratio and speed, which means the compressed data remains the same after decompressing.
The gzip command uses a deflate algorithm which is a lossless data compression that creates smaller file size to make file transfer much faster, as compared to other compression tools.
In this guide, we are going to discuss gzip command usage with examples in Linux.
gzip Command Syntax
Just like any other Linux command, the gzip command’s syntax is divided into two parts OPTIONS and FILES.
$ gzip [OPTIONS]... [FILES]...
In the above syntax, the OPTIONS are used to alter the behavior of the command whereas the FILES represent the input files.
1. Create a Gzip File in Linux
One of the very common uses of the gzip command is to compress a large file. It is very common to see that, large ISO files or tar bundles are compressed to save disk space.
To compress a single file, we just need to pass the file name to the gzip command. To understand this, let’s use the following command to compress the ISO file:
$ gzip alma-linux.iso
Now, let’s use the ls command to verify that the file has been compressed:
$ ls -l
In the above output, we can see the new compressed file with the name alma-linux.iso.gz. We can also observe that, by default, the gzip command adds a
.gz extension to a compressed file.
2. Create Gzip and Keep Original File
In the previous example, we saw how easy it is to compress a file. However, if we observe carefully then we can notice that the gzip command deletes the original file after compressing it.
However, sometimes we want to keep the original file as well. In such cases, we can use the
-k option as shown.
$ gzip -k alma-linux.iso $ ls -l
In the above output, we can see that the original file is intact even after the compression.
3. View Contents of a .gz File
To view the contents of a compressed
.gz file, use the zcat command – which allows you to view the compressed file contents without uncompressing.
$ zcat alma-linux.iso
4. View Info of a .gz File
Sometimes, we want to display more details about the compressed file. In such a case, we can use the
-l option to list the following fields:
- compressed size – it represents the size of the compressed file in bytes.
- uncompressed size – it represents the size of the uncompressed file in bytes.
- ratio – it represents the compression ratio.
- uncompressed_name – it represents the name of the uncompressed file.
To understand this, let’s execute the following command:
$ gzip -l alma-linux.iso.gz
5. Overwrite Gzip File Without Confirmation
By default, the gzip command works in an interactive way if the compressed file with the same name already exists. To understand this default behaviour, let’s execute the same command from the previous example:
$ gzip -k alma-linux.iso
Here, we can see that the gzip command waits for the user input. Now, we can use the
'y' to overwrite the file or
'n' to abort the operation.
This interactive operation is safe and avoids overwriting files by mistake. However, this is not suitable every time. For example, if we are executing the gzip command from the script then the script will wait infinitely for the user input. In such cases, we can use the
-f option which overwrites the files forcefully.
Now, let’s execute the same command with the
$ gzip -f -k alma-linux.iso
Here, we can see that now the gzip command works in a non-interactive way.
6. Compress Multiple Files with Gzip
So far we saw how to compress a single file. In a similar way, we can use the gzip command to compress multiple files at once.
So, first, let’s create multiple copies of the file using the following cp command:
$ cp alma-linux.iso alma-linux-1.iso $ cp alma-linux.iso alma-linux-2.iso $ cp alma-linux.iso alma-linux-3.iso
Next, let’s compress the three files using the below command:
$ gzip alma-linux-1.iso alma-linux-2.iso alma-linux-3.iso $ ls -l
7. Gzip All Files in a Directory
In the previous example, we saw how to compress multiple files. In a similar way, we can also compress all the files from a directory.
To understand this, first let us create a new directory and add a few files into it:
$ mkdir dir-1 $ touch dir-1/file-1.txt dir-1/file-2.txt dir-1/file-3.txt
Now, let’s use the
-r option to compress all the files from the dir-1 directory:
$ gzip -r dir-1/ $ ls -l dir-1/
In the above example, the
-r option traverses the directory in a recursive fashion.
8. Decompress a Gzip File in Linux
The gzip command allows us to decompress the file using the
-d option as shown.
$ gzip -d alma-linux.iso.gz $ ls -l
9. Compress Tar File to Gzip
In the previous two examples, we saw how to compress multiple files using a single command. However, we can also observe that the gzip command doesn’t compress these files into a single file. In such cases, first, we can create a tar bundle and then compress it using the gzip command.
So, first, let’s create a tar bundle with multiple files in it:
$ tar cvf sample.tar alma-linux-1.iso alma-linux-2.iso
Now, let us compress this tar bundle using the following command:
$ gzip sample.tar $ ls -l
10. Speeding Up gzip Compression
So far, we used very small files to demonstrate the usage of the gzip command. However, in real scenarios, the data that needs to be compressed can be really large.
In such cases, we can use the
--fast option to reduce the compression time.
$ gzip --fast alma-linux.iso
It is important to note that, the
--fast option gives preference to the compression speed over the ratio.
11. Speeding Up Gzip Compression Ratio
In a similar way, we can use the
--best option to improve the compression ratio. To understand this, let’s execute the below command:
$ gzip --best alma-linux.iso
Here, we should note that the
--best option gives preference to the compression ratio over the speed.
12. Set Gzip Compression Level
We can use an integer argument with the gzip command to regulate the speed of the compression. The valid value for the range is between 1 to 9. The value 1 represents the fasted compression whereas the value 9 represents the slowest compression.
For example, the following command uses the 2 as an argument to improve the compression speed:
$ gzip -2 alma-linux.iso
It is important to note that, the default compression level in gzip is -6. It prefers high compression at the expense of speed.
13. Change Gzip File Extension Suffix
By default, the gzip command uses the
.gz suffix after compressing the file. However, we can override this default behavior using the
For example, we can use the below command to use gnuzip as a suffix:
$ gzip --suffix .gnuzip alma-linux.iso $ ls -l
In the above example, we can see the compressed file has a
In this practical guide, we discussed some of the common examples of the gzip command in Linux to compress and decompress files faster.
Do you know of any other best example of the gzip command in Linux? Let us know your views in the comments below.