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.

 

bash code
bzip2 file.txt
[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
bash code
bzip2 file1.txt file2.txt file3.txt

 Compress a single file and keep the original

  • You can instead keep the original file and create a compressed copy.
bash code
bzip2 -c file.txt > file.txt.bz
  • 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.
bash code
                                                                            bzip2 -k file.txt

 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.
bash code
  bzip2 -d file.txt.bz2

OR

bash code
 bunzip2 file.txt.bz2
  • 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.
bash code
bunzip2 -c file.txt.bz2 > file.txt
[ad type=”banner”]

OR

bash code
bunzip2 -k file.txt.bz2

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.
bash code
[root@centos ~]# bzip2 -v linux-3.18.19.tar
linux-3.18.19.tar: 6.015:1, 1.330 bits/byte, 83.37% saved, 580761600 in, 96552670 out.

[root@centos ~]# ls -lah
-rw-r--r--. 1 root root 554M Jul 22 10:38 linux-3.18.19.tar
-rw-r--r--. 1 root root 93M Jul 22 10:38 linux-3.18.19.tar.bz2
  • 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.
bash code
tar cjvf etc.tar.bz2 /etc/
  •  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.
bash code
bzip2 -c file1.txt > files.bz2
bzip2 -c file2.txt >> files.bz2
  • 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: