bash bzip2 - ghdrako/doc_snipets GitHub Wiki

bzip2 is a powerful compression tool that compresses files using the Burrows-Wheeler block sorting text compression algorithm, followed by Huffman coding. This algorithm allows bzip2 to achieve a high level of compression while maintaining a relatively fast decompression time. This makes bzip2 ideal for compressing large files, such as backups, databases, and software distributions. When a file is compressed using bzip2, it is typically given a .bz2 extension.

 tar -cvf - directory_to_backup | bzip2 -9 -c > backup.tar.bz2

The -9 option specifies the highest compression level, while the -c option sends the output to stdout.

compression levels in bzip2:

  • -1 (fastest): This level provides the fastest compression but generates larger compressed files. It’s suitable for situations where speed is more critical than file size reduction.
  • -9 (maximum compression—our choice in Figure 3.13): This level offers the highest compression, resulting in smaller compressed files. However, it is slower than lower levels. It’s useful when saving disk space is a top priority and you don’t mind waiting for the compression process to finish.
  • -2 to -8 (intermediate levels): These levels offer a trade-off between compression speed and resulting file size. As you move from -2 to -8, the compression becomes better (smaller file size) but slower compared to lower levels. Choose an intermediate level based on your specific requirements for speed and file size reduction.