gzip / bzip2 - data compression programs

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 has been written as a replacement of 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)]

  gzip -9 file.tar    - compresses file.tar into file.tar.gz
  gzip -d file.tar.gz - decompresses file.tar.gz to file.tar


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 on
              the command line 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

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 recompress
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.