How to use gzip and bzip2 Linux commands Explained
The gzip and bzip2 are compression utilities on Linux. Linux provides both with default installation. Both commands use similar syntax and options to compress and decompress files.
The gzip command uses the following syntax to compress a file.
#gzip [option] [file]
The bzip2 command uses the following syntax to compress a file.
#bzip2 [option] [file]
The -h option lists all the supported options. It works with both commands. The following command lists all supported options of the gzip command.
#gzip -h

The following command lists all supported options of the bzip2 command.
#bzip2 -h

Compressing and decompressing files
To compress a file, specify its name (if it is in the same directory) or the full path (if it is in another directory) with the gzip and bzip2 commands. For example, to compress the file_a file, you can use any command from the following commands.
#gzip file_a #bzip2 file_a
Gzip uses the .gz extension to represent the compressed file. Bzip2 uses the .bz2 extension for it. Both commands replace the source file with the compressed file. Both commands support two methods for decompression: an option and a separate command. Both use the -d option to decompress the compressed file. Both also offer separate commands for this. These commands are the gunzip (for gzip) and bunzip2 (for bzip2) . You can use any option to decompress the file. For example, to decompress the file file_a.bz2, you can use any command from the following commands.
#bzip2 -d file_a.bz2 #bunzip file_a.bz2
To decompress the file file_a.gz, you can use any 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.

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

Redirecting output to a device or file
By default, both commands save the compressed file in the current directory. The option -c allows you to override this behaviour. It lets you save the compressed file in another location. If you use the option -c without specifying a location or device, the command dumps the output on 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

You can use this feature to create a single compressed file from multiple files.
Getting information from a compressed file
The -l option scans the supplied compressed file and lists the following information.
Compressed size, uncompressed size, compression ratio, and uncompressed name

This option only works with the gzip command. The bzip2 does not support this option.
Compressing files recursively
The -r option scans and compresses all files from a directory and subdirectories. The following command recursively compresses all files from the directory a_dir.
#gzip -r a_dir
You can also use this option with the gunzip command to decompress all files recursively.
#gunzip -r a_dir

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 original file

This option only works with the bzip2 command. The gzip command does not support this option.
Recovering a damaged compressed file
Bzip2 provides the bzip2recover tool. It allows you to recover a damaged compressed file. It scans the damaged file, skips the corrupt data blocks, and copies the remaining ones into a new file.
Compress a file using the bzip2 command, open the compressed file using a text editor, add an extra line and save the file.
Now, the file contains text in compressed and decompressed formats. The bzip2 treats this file as a corrupt compressed file.
To repair it, you can use the bzip2recover tool. After repairing,you can use bzip2 to decompress it.
Recovering corrupt file

The bzip2recover tool works only with bzip2 compression. It can not recover a file compressed using another utility.
Adjusting speed and compression ratio
You can adjust the speed and the compression ratio in both commands. Both commands use a scale of one to nine; one provides the highest speed but the lowest compression ratio, while nine provides the highest compression ratio but the lowest speed. The compression ratio works in the inverse of the speed. The default value is six. To use any other value, specify that value as the option.

Compressing an already compressed file
When we compress a file, the command saves the information needed to decompress the file with the compressed data. If we compress the file again, it adds this information again. Since the data is already in the compressed format, it remains unchanged. Thus, compressing an already compressed file increases the file instead of decreasing it. The command does not compress an already compressed file. You need to use the -f option to do this forcefully.

Compress a file with the gzip command and note the compressed file size. Now compress it again using the -f option.

A file compressed two times also needs to be decompressed two times.
This tutorial is the first part of the tutorial 'The gzip and bzip2 commands in Linux Explained with Examples'. The last part of this tutorial is the following.
Similarities and differences between gzip and bzip2
Conclusion
Compression reduces file size without losing data. A compressed file consumes less disk space. Gzip and bzip2 are built-in utilities for file compression on Linux. This tutorial explained both tools through various examples.
By ComputerNetworkingNotes Updated on 2025-12-02