4.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. You might also use another compressing tool, programs like zip, arj or rar also exist for GNU/Linux (but they are rarely used).

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.

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:

Warning

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 image archives with someone, but he doesn't have 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 files right away, 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. 2002.
http://www.mandrakelinux.com/