bzip2 is one of the compression method in Linux, high-quality data compressor.
It typically compresses files to within 10% to 15% of the best available techniques (the PPM family of statistical compressors).
It compresses the normal file and stores into single .bz2
extension file, this compression is much better compared with gzip compression.
bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding.
The command-line options are deliberately very similar to those of GNU gzip, but they are not identical.
bzip2 expects a list of file names to accompany the command-line flags. Each file is replaced by a compressed version of itself, with the name “original_name.bz2”.
Each compressed file has the same modification date, permissions, and, when possible, ownership as the corresponding original, so that these properties can be correctly restored at decompression time.
How To Install bzip2 In Linux?
We can easily install it with help of package manager since the package is available in all the Linux distributions repository.
For Fedora
system, use DNF Command to install bzip2.
$ sudo dnf install bzip2
For Debian/Ubuntu
systems, use APT-GET Command or APT Command to install bzip2.
$ sudo apt install bzip2
For Arch Linux
based systems, use Pacman Command to install bzip2.
$ sudo pacman -S bzip2
For RHEL/CentOS
systems, use YUM Command to install bzip2.
$ sudo yum install bzip2
For openSUSE Leap
system, use Zypper Command to install bzip2.
$ sudo zypper install bzip2
Syntax:
Common syntax for bzip2 and bunzip2.
[bzip2 (Compress) syntax] # bzip2 [Files To Be Compressed] [bzip2 (Decompress) syntax] # bunzip2 [FileName.bz2] [bzip2 (Decompress) syntax] # bzip2 -d [FileName.bz2] [bzip2 (Decompress) syntax] # bzcat -dc [FileName.bz2]
1) How to compress the file using bzip2 command
The below example compress the “test-bzip2.txt” file to test-bzip2.txt.bz2 file. See the output.
# bzip2 test-bzip2.txt
Here i’m going to check the mentioned file was compressed or not. See the output.
# ll -h
total 56K
drwx------. 6 mageshm mageshm 4.0K Dec 31 09:00 ./
drwx--x--x. 16 root root 4.0K Dec 24 01:16 ../
-rw-------. 1 mageshm mageshm 1.3K Dec 27 15:05 .bash_history
-rw-r--r--. 1 mageshm mageshm 18 May 10 2012 .bash_logout
-rw-r--r--. 1 mageshm mageshm 176 May 10 2012 .bash_profile
-rw-r--r--. 1 mageshm mageshm 124 May 10 2012 .bashrc
-rw-r--r--. 1 root root 26 Dec 27 12:25 liks
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 08:54 links/
drwxrwxr-x. 2 mageshm mageshm 4.0K Nov 16 10:14 perl5/
drwxr-xr-x. 2 mageshm mageshm 4.0K Nov 16 10:07 public_ftp/
drwxr-xr-x. 3 mageshm mageshm 4.0K Nov 16 10:07 public_html/
lrwxrwxrwx. 1 root root 31 Dec 7 09:41 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx. 1 root root 38 Dec 7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder
-rw-r--r--. 1 root root 61 Dec 27 13:35 test2-bzip2.txt
-rw-r--r--. 1 root root 61 Dec 31 08:54 test3-bzip2.txt
-rw-r--r--. 1 root root 92 Dec 27 13:31 test-bzip2.txt.bz2
2) How to Unzip/de-compress the bzip2 file
The below command is Unzip/de-compress the bzip2 file. See the below output.
# bunzip2 test-bzip2.txt.bz2 or # bzip2 -d test-bzip2.txt.bz2
Here i’m going to check the file whether de-compressed or not. see the output.
# ll -h total 56K drwx------. 6 mageshm mageshm 4.0K Dec 31 09:11 ./ drwx--x--x. 16 root root 4.0K Dec 24 01:16 ../ -rw-------. 1 mageshm mageshm 1.3K Dec 27 15:05 .bash_history -rw-r--r--. 1 mageshm mageshm 18 May 10 2012 .bash_logout -rw-r--r--. 1 mageshm mageshm 176 May 10 2012 .bash_profile -rw-r--r--. 1 mageshm mageshm 124 May 10 2012 .bashrc -rw-r--r--. 1 root root 26 Dec 27 12:25 liks drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 08:54 links/ drwxrwxr-x. 2 mageshm mageshm 4.0K Nov 16 10:14 perl5/ drwxr-xr-x. 2 mageshm mageshm 4.0K Nov 16 10:07 public_ftp/ drwxr-xr-x. 3 mageshm mageshm 4.0K Nov 16 10:07 public_html/ lrwxrwxrwx. 1 root root 31 Dec 7 09:41 s-link-file -> /links/soft-link/test-soft-link lrwxrwxrwx. 1 root root 38 Dec 7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder -rw-r--r--. 1 root root 61 Dec 27 13:35 test2-bzip2.txt -rw-r--r--. 1 root root 61 Dec 31 08:54 test3-bzip2.txt -rw-r--r--. 1 root root 60 Dec 27 13:31 test-bzip2.txt
Hope this article was useful for you. Kindly provide your valuable feedback/comments in the commenting section.
Stay tuned with us !!
thanks for examples posted here, trying to learn some compression methodologies and i found this helpful as well as some additional bzip2 examples if anyone is also interested.
https://www.rootusers.com/10-simple-bzip2-examples/
http://linux.about.com/library/cmd/blcmdl1_bzip2.htm
good luck!
@jon,
Welcome.