16.6. bzip2 and gzip: data compression programs

You can see that we already have talked of these two programs when dealing with tar. Unlike WinZip under Windows, archiving and compressing are done using two separate utilities -- tar for archiving, and the two programs which we will now introduce for compressing data: bzip2 and gzip.

At first, bzip2 was written as a replacement for gzip. Its compression ratios are generally better, but on the other hand it is more memory-greedy. The reason why gzip is still here is that it is still more widespread than bzip2. Maybe bzip2 will eventually replace gzip, but maybe not.

Both commands have a similar syntax:

gzip [options] [file(s)]

If no filename is given, both gzip and bzip2 will wait for data from the standard input and send the result to the standard output. Therefore, you can use both programs in pipes. Both programs also have a set of common options:

  1. -1, ..., -9: Set the compression ratio. The higher the number, the better the compression, but better also means slower: "There's no such thing as a free lunch".

  2. -d: Uncompress file(s). This is equivalent to using gunzip or bunzip2.

  3. -c: Dump the result of compression/decompression of files given on the command line to the standard output.

Watch out! By default, both gzip and bzip2 erase the file(s) that they have compressed (or uncompressed) if you don't use the -c option. You can avoid it with bzip2 by using the -k option, but gzip has no such option!

Now some examples. Let's say you want to compress all files ending with .txt in the current directory using bzip2, you will then use:

$ bzip2 -9 *.txt

Let's say you want to share your images archive with someone, but he hasn't got bzip2, only gzip. You don't need to uncompress the archive and re-compress it, you can just uncompress to the standard output, use a pipe, compress from standard input and redirect the output to the new archive:

bzip2 -dc images.tar.bz2 | gzip -9 >images.tar.gz

And here you are. You could have typed bzcat instead of bzip2 -dc. There is an equivalent for gzip but its name is zcat, not gzcat. You also have bzless (resp. zless) if you want to view compressed file directly, without having to uncompress them first. As an exercise, try and find the command you would have to type in order to view compressed files without uncompressing them, and without using bzless or zless :-)


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2000.
http://www.linux-mandrake.com/