Bzip2:

  • bzip2 is a free and open-source file compression program that uses the Burrows–Wheeler algorithm.
  • It only compresses single files and is not a file archiver. It is developed and maintained by Julian Seward.
  • The bzip2 command is used for compressing and decompressing files.
  • Data compression, also referred to as just compression, is the process of encoding data using fewer bits.
  • Data decompression, or just decompression, is the process of restoring compressed data back into a form in which it is again useful.
  • bzip2 features a high rate of compression together with reasonably fast speed.
  • Most files can be compressed to a smaller file size than is possible with the more traditional gzip and zip programs.
  • Compression is performed even if the compressed file is larger than the original.
  • This situation can occur in the case of very small files, because the compression mechanism has a fixed overhead of about 50 bytes.

Compress a single file

  • This will compress file.txt and create file.txt.bz2, note that this will remove the original file.txt file.

 

[pastacode lang=”bash” manual=”bzip2%20file.txt%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]

Compress multiple files at once

  • This will compress all files specified in the command, note again that this will remove the original files specified by turning file1.txt, file2.txt and file3.txt into file1.txt.bz2, file2.txt.bz2 and file3.txt.bz2
[pastacode lang=”bash” manual=”bzip2%20file1.txt%20file2.txt%20file3.txt%0A” message=”bash code” highlight=”” provider=”manual”/]

 Compress a single file and keep the original

  • You can instead keep the original file and create a compressed copy.
[pastacode lang=”bash” manual=”bzip2%20-c%20file.txt%20%3E%20file.txt.bz%0A” message=”bash code” highlight=”” provider=”manual”/]
  • The -c flag outputs the compressed copy of file.txt to stdout, this is then sent to file.txt.bz2, keeping the original file.txt file in place.
  • The version of bzip2 that I am testing with, 1.0.6, which is currently the latest available as of this writing also has the -k option which keeps the original file, so alternatively you could also run the below command to get the same result.
[pastacode lang=”bash” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bzip2%20-k%20file.txt%0A” message=”bash code” highlight=”” provider=”manual”/]

 Decompress a bzip2 compressed file

  • To reverse the compression process and get the original file back that you have compressed, you can use the bzip2 command itself or bunzip2 which is also part of the bzip2 package.
[pastacode lang=”bash” manual=”%20%20bzip2%20-d%20file.txt.bz2%0A” message=”bash code” highlight=”” provider=”manual”/]

OR

[pastacode lang=”bash” manual=”%20bunzip2%20file.txt.bz2%0A” message=”bash code” highlight=”” provider=”manual”/]
  • Both of these commands will produce the same result, decompressing file.txt.bz2 to file.txt, removing the compressed file.txt.bz2 file.
  • Similar to example 3, it is possible to decompress a file and keep the original .bz2 file as below.
[pastacode lang=”bash” manual=”bunzip2%20-c%20file.txt.bz2%20%3E%20file.txt%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]

OR

[pastacode lang=”bash” manual=”bunzip2%20-k%20file.txt.bz2″ message=”bash code” highlight=”” provider=”manual”/]

List compression information

  • With the -v or –verbose flag we can see useful information regarding the compression ratio of a file, which shows us how much disk space our compression is saving. Additional ‘v’ flags can be added for more in depth information.
[pastacode lang=”bash” manual=”%5Broot%40centos%20~%5D%23%20bzip2%20-v%20linux-3.18.19.tar%0A%20%20linux-3.18.19.tar%3A%20%206.015%3A1%2C%20%201.330%20bits%2Fbyte%2C%2083.37%25%20saved%2C%20580761600%20in%2C%2096552670%20out.%0A%0A%5Broot%40centos%20~%5D%23%20ls%20-lah%0A-rw-r–r–.%201%20root%20root%20554M%20Jul%2022%2010%3A38%20linux-3.18.19.tar%0A-rw-r–r–.%201%20root%20root%20%2093M%20Jul%2022%2010%3A38%20linux-3.18.19.tar.bz2%0A” message=”bash code” highlight=”” provider=”manual”/]
  • In this example, a bzipped copy of the Linux kernel has compressed to 83.87% of its original size, taking up 93MB of space rather than 554MB.

Compress a directory   

  • With the help of the tar command, we can create a tar file containing a whole directory and compress the result with bzip2.
  • We can perform the whole lot in one step, as the tar command allows us to specify a compression method to use.
[pastacode lang=”bash” manual=”tar%20cjvf%20etc.tar.bz2%20%2Fetc%2F%0A” message=”bash code” highlight=”” provider=”manual”/]
  •  This example creates a compressed etc.tar.bz2 file of the entire /etc/ directory.
  • The tar flags are as follows, ‘c’ creates a new tar archive, ‘j’ specifies that we want to compress with bzip2, ‘v’ provides verbose information, and ‘f’ specifies the file to create.
  • The resulting etc.tar.bz2 file contains all files within /etc/ compressed using bzip2.

Concatenate multiple files

  • Multiple files can be concatenated into a single .bz2 file.
[pastacode lang=”bash” manual=”bzip2%20-c%20file1.txt%20%3E%20files.bz2%0Abzip2%20-c%20file2.txt%20%3E%3E%20files.bz2%0A” message=”bash code” highlight=”” provider=”manual”/]
  • The files.bz2 now contains the contents of both file1.txt and file2.txt, if you decompress files.bz2 you will get a file named ‘files’ which contains the content of both .txt files.
  • The output is similar to running ‘cat file1.txt file2.txt’.
  • If instead you want to create a single file that contains multiple files you can use the tar command which supports bzip2 compression,

 

Categorized in: