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:
-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";
-d: uncompress file(s). This is equivalent to using gunzip or bunzip2;
-c: dump the result of compression/decompression of files given as parameters to the standard output.
![]() | 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 |
bzip2 -dc images.tar.bz2 | gzip -9 >images.tar.gz |