How to use gzip and bzip2 Linux commands Explained

This tutorial explains how to compress and decompress files in Linux by using the gzip and bzip2 commands. Learn how to use the gzip and bzip2 Linux commands through practical examples.

The gzip and bzip2 commands not only work similarly but also use similar syntax and options to compress and decompress files.

For example, the gzip command uses the following syntax.

#gzip [option] [file]

Just like the above syntax, the bzip2 command uses the following syntax.

#bzip2 [option] [file]

By using the -h option, you can list all the supported options of both commands.

The following image shows all the supported options of the gzip command.

all options of gzip command

The following image shows all the supported options of the bzip2 command.

all options of bzip2 command

Compressing and decompressing files

Compressing and decompressing files with the gzip and bzip commands are relatively simple. To compress a file, specify its name (if the file is in the same directory) or the full path (if the file is in another directory) with these commands. For example, to compress a file named file_a, you can use anyone command from the following commands.

#gzip file_a
#bzip2 file_a

The gzip command uses the .gz extension to represent the compressed file while bzip2 uses the .bz2 extension to represent the compressed file. Once the compression is done, both commands replace the source file with the compressed file.

To decompress the compressed file, both commands support two methods: a command option and a separate command.

Both commands use the -d option to decompress the compressed file. Both commands also offer commands to decompress the compressed file. These commands are the gunzip and bunzip2 for the gzip and bzip2 utilities, respectively.

You can use the corresponding command or can use the command’s inbuilt functionality.

For example, to decompress the file file_a.bz2, you can use anyone command from the following commands

#bzip2 -d file_a.bz2
#bunzip file_a.bz2

To decompress the file file_a.gz, you can use anyone command from the following commands.

#gzip -d file_a.gz
#gunzip file_a.gz

The following image shows the compression and decompression with the gzip and gunzip commands.

compress and decompress with gzip command

The following image shows the compression and decompression with the bzip2 and bunzip commands.

compression and decompression from bzip2

Redirecting output to a device or file

By default, both commands store output to a new compressed file. And once compression is done, both commands replace the supplied file with the compressed file.

If required, you can store the output on a custom location such as a file or a device. To send the output to a custom location, the option -c is used. The option -c forces the command to send the output to a custom location and keeps the original file.

The following image shows how to use the -c option through an example.

saving output to custom location

If the option -c is used without specifying any location or device, the command will use the default output device. Since the default output device is the console, the command writes the output to the console.

You can also use shell redirection (>) to store the output to a custom location. For example, the following command compresses two files: small-file and small-file-2 in the supplied sequence and writes the output to a new file named small.gz.

#gzip -c small-file small-file-2 > small.gz

The following image shows the above command with the output.

combining output

You can also use this feature to create a single compressed file from multiple files.

Getting information from a compressed file

The gzip command, if used with the -l option, scans the supplied compressed file and lists the following information about that file.

Compressed size, uncompressed size, compression ratio, and uncompressed name

The following image shows how to use this option.

gzip compression ratio

This option only works with the gzip command. The bzip2 doesn’t support this option.

Compressing files recursively

To scan and compress all files from a directory and all of its subdirectories, the -r option is used.

For example, the following command not only compresses all files in a directory named a_dir but also scans all of its subdirectories. If it finds a file in any sub-directory, it will also compress that file.

#gzip -r a_dir

You can also use this option with the gunzip command to decompress all files from a directory and all of its sub-directories recursively.

#gunzip -r a_dir

The following image shows how to use the -r option through an example.

gzip recursive compression

The bzip2 command does not support the recursive operation.

Keeping the original file intact

By default, the bzip2 command replaces the supplied input file with a compressed output file. To keep the input file intact, use the -k option with the bzip2 command.

For example, the following command compresses a file named file_a but does not delete the original file after compressing it.

#bzip2 -k file_a

The following image shows the above command with the output.

keeping orginal file

This option only works with the bzip2 command. The gzip command does not support this option.

Recovering a damaged compressed file

To recover a damaged compressed file, bzip2 provides a separate tool known as the bzip2recover. This tool scans the damaged file, skips the corrupt data blocks, and copies the correct data blocks into a new file. To understand how this process works, let’s take an example.

Compress a file by using the bzip2 command and open the compressed file by using a text editor. Add an extra line in the file and save the file.

Now the file contains text in both formats: compressed and decompressed. The bzip2 treats this file as a corrupt compressed file.

To repair this file, you can use the bzip2recover tool. Once the file is repaired, it can be decompressed with the bzip2.

The following image shows this example with the output.

bzip recovering corrupt file

The bzip2recover tool works only with bzip2 compression. A file that is compressed with any other utility or tool can’t be repaired by using this tool.

Adjusting speed and compression ratio

We can adjust the speed and the compression ratio in both commands. Both commands use a scale of one to nine where number one provides the highest speed but the lowest compression ratio, while number nine provides the highest compression ratio but the lowest speed. The compression ratio works in inverse of the speed.

The default value is set to six. To use any other value, specify that value as the option.

The following image shows how changing this value can affect the compression ratio.

compression ratio

Compressing an already compressed file

When we compress a file, the information needed to decompress the file is also stored with the compressed data. If we compress the file again, this information will be added again. Since the data is already compressed, the data will remain unchanged for the second time. So if we compress an already compressed file, we will end up with a larger file.

Although it’s a waste of time and disk space, yet if required, you can compress a compressed-file again with the -f option.

Let’s take an example. Compress a file with the gzip command and note down the size of the compressed file. Now compress the file again. Since the file has already compressed, the gzip will not compress the file again. Use the option -f, to do it forcefully. Once the file is compressed again, compare its size with the noted size.

The following image shows this exercise with the output.

compressing two times

A file compressed two times also needs to be decompressed two times.

That’s all for this tutorial. In the next part of this tutorial, we will discuss the similarities and differences between the gzip and bzip2 commands. If you like this tutorial, please don’t forget to share it with friends through your favorite social channel.

Advertisements

ComputerNetworkingNotes Linux Tutorials How to use gzip and bzip2 Linux commands Explained