Similarities and differences between gzip and bzip2

A compressed file uses less disk space. It consumes less memory and network bandwidth when moved to another location. Gzip and bzip2 are the two most widely used compression utilities on Linux. Linux provides both tools with the default installation. This tutorial compares both and explains their similarities and differences.

Similarities and differences between the gzip and bzip2

The following table compares the supported options of gzip and bzip2.

Short option Long option Supported command Description
-h --help Both List all supported options.
-d --decompress Both Decompress the compressed file.
-f --force Both Overwrite the existing output file.
-t --test Both Test the compressed file's integrity.
-c --stdout Both Write the output to the standard output device.
-q --quiet Both Don't display non-critical errors and warnings.
-v --verbose Both Display verbose messages.
-L --license Both The bzip2 displays both the software version and license information. The gzip displays License information only.
-V --version Both The bzip2 displays both software version and license information. The gzip shows version information only.
-1 --fast Both The bzip2 sets block size to 100k. The gzip compresses faster.
-9 --best Both The bzip2 sets block size to 900k. The gzip compresses better.
-z --compress bzip2 only Force compression.
-k --keep bzip2 only Keep the original file.
-s --small bzip2 only Use less memory.
-l --list gzip only Display the compressed and decompressed size.
-n --no-name gzip only Do not save or restore the original name and time stamp.
-N --name gzip only Save or restore the original name and time stamp.
-r --recursive gzip only Operate recursively on directories.
-S --suffix=SUF gzip only Use suffix SUF on compressed files.
  • Options -h, -d, -f, -t, -c, -q and -v work similarly in both commands.
  • Options -1, -9, -L and -V work differently in both commands.
  • Options -z, -k and -s work only with the bzip2 command.
  • Options -l, -n, -S, -N and -r work only with the gzip command.

Difference between gzip and bzip2 commands

Besides options, there are a few more differences between both commands. The following table lists those differences.

The gzip command The bzip2 command
It uses the DEFLATE algorithm. It uses the Burrows-Wheeler block sorting algorithm.
To denote the compressed file, it uses the extension .gz. To denote the compressed file, it uses the extension .bz2.
It compresses files at a higher speed than the bzip2 command. It provides a higher compression ratio than the gzip command.
It does not provide any built-in functionality or associate program to recover the damaged .gz files. It provides an additional program called bzip2recover that can recover the damaged .bz2 files.
For decompression, it provides the gunzip utility. For decompression, it provides the bunzip2 utility.
It supports recursive compression. It does not support recursive compression.

Comparing compression time, size, and ratio

The bzip2 provides a higher compression ratio but takes more time. The gzip takes less time but also provides less compression. Let us verify this through an example.

  • Create a directory and switch the current working directory to it.
  • Create a test file by redirecting the man page of the grep command.
  • Check the file size.
  • Use the gzip command to compress the file. Note down the time it takes.
  • Check the size of the compressed file and uncompress it.
  • Compress it again using the bzip2 command and note down the time taken.
  • Check the compressed file size and uncompress it for the next exercise.
#mkdir testdir
#cd testdir
#man grep > testfile
#du -b testfile
#time gzip testfile
#ls
#du -b testfile.gz
#gunzip testfile.gz
#ls
#du -b testfile
#time bzip2 testfile
#du -b testfile.bz2
#time bzip2 testfile
#du -b testfile.bz2
#bunzip2 testfile.bz2
#du -b testfile
#

comparing comression

To check and verify the compression ratio, use the -v switch with both commands.

#du -b testfile
#gzip -v testfile
#gunzip testfile.gz
#bzip2 -v testfile
#bunzip2 testfile.bz2

compression ratio

This tutorial is the last part of the tutorial 'The gzip and bzip2 commands in Linux Explained with Examples'. The first part of this tutorial is the following.
How to use gzip and bzip2 Linux commands Explained

Conclusion

This tutorial compared gzip and bzip2 and explained their similarities and differences through examples. By learning these similarities and differences, you can effectively select and use the tool that fits your requirements.

ComputerNetworkingNotes Linux Tutorials Similarities and differences between gzip and bzip2

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com