How to Compress and Decompress .bz2 files in Linux Using bzip2 Command

Introduction

In this article we are going to learn How to compress files using bz2 file compression tool (bzip2 linux command) and unzip bz2 compressed file. bzip2 is a open source compress tool similar to zip & gzip compression tool used to compress large sized files to reduce it’s size. bzip2 can compress files not directories. bzip2 gives more compression as compared to gzip and zip. bzip2 Linux command was developed by Julian Seward on year 1996 and released under BSD style license.

Why we need to compress a file. Let’s take an example. Suppose you want to send a mail to someone with an attachment. but your attachment file size is larger then the allowed attachment size limit. In that case you can use the bz2 file compression tool (bzip2 linux command) to compress the file to reduce the size of the file. Some major features of bz2 file compression tool includes :

  • It’s and cross platform application available for major operating system i.e. Linux, Microsoft Windows, MacOS.
  • Can recover data from corrupted bz2 file.
  • Available for both 32 bit and 64 bit operating system.
  • Can create fast and best compression of the files.

Now let’s have a look at bzip2 Linux command with examples:

Compress a File

Compress a file using bzip2 Linux command.

$ bzip2 myfile.txt   # Compress a File

Output:

$ ls
myfile.txt.bz2

Compress a file with a Standard Output

bzip2 command with -c will compress the file with a standard output.

$ bzip2 -c myfile.txt > myfile.txt.bz2  # Compress a file with standard output

Output:

$ ls
myfile.txt  myfile.txt.bz2

Compress a file by keeping the Input file (Source File)

Normally bzip2 command compress the file and deletes the Source file but bzip2 command with argument -k will compress the file by keeping the Source file undeleted.

$ bzip2 -k myfile.txt   # Compress file without delete Input file

Output:

$ ls
myfile.txt  myfile.txt.bz2

Compress multiple files at once

bz2 file compression tool (bzip2 linux command) can compress multiple files at once. To do so use the below command.

$ bzip2 myfile.txt myfile1.txt myfile2.txt myfile3.txt   # Compress multiple files at once

Output:

$ ls
myfile1.txt.bz2  myfile2.txt.bz2  myfile3.txt.bz2  myfile.txt.bz2

Check integrity of a specified file

bzip2 linux command with argument -t checks the integrity of a specified bz2 file. Integrity in the sense the bz2 file is a valid file or not. You can do so using below command. If the file is valid then you will get no output.

$ bzip2 -t myfile.txt.bz2   # Check the Integrity of the bz2 file

But if the bz2 file is not a valid file you will get error. To prove so let’s create a bzip2 file using touch command and then check for integrity.

$ touch myfile.txt.bz2
$ bzip2 -t myfile.txt.bz2 
bzip2: myfile.txt.bz2: file ends unexpectedly

You can use the “bzip2recover” program to attempt to recover data from undamaged sections of corrupted files. As you can see above we got the error because we didn’t created this file using bzip2 linux command hence it is not a valid bz2 file.

Uncompress/Extract/Unzip bz2 file

bzip2 command with argument -d will uncompress bzip2 file.

$ bzip2 -d myfile.txt.bz2   # Uncompress a file

Output:

$ ls
myfile.txt

You can also use bunzip2 command to unzip bz2 file. Refer the command below.

$ bunzip2 myfile.txt.bz2 

Output:

$ ls
myfile.txt

Check content of a bz2 compressed file using bzcat command

You can check content of a compressed file without extracting it. To do so we have to use bzcat command. Refer the command below.

$ bzcat myfile.txt.bz2   # Check content of a Compressed file
Welcome to thegeekdiary.com

Compress a file Forcefully

bzip2 command with argument -f will create a bz2 file forcefully. Refer the command below.

$ bzip2 -f myfile.txt    # Compress a file forcefully
$ ls
myfile.txt.bz2

Compress a directory with bz2 file compression tool

By using the only bzip2 linux command we cannot compress a directory but bzip2 command with tar command can compress a directory. Use the below command to do the same.

$ tar -cjvf data.tar.bz2 data/   # Compress a Directory
data/
data/myfile3.txt
data/myfile2.txt
data/myfile1.txt

Output:

$ ls
data  data.tar.bz2

Here argument -j is for bzip2 compression.

Level’s of Compression

There are 9 level of compression are available in every compression tool. The level’s are 1,2…9. Here I will show you two level’s. i.e. Level 1 & Level 9.

Level 1 gives fast compression output. Refer the below command.

$ bzip2 -1 myfile.txt   # 1 for Fast Compression

Level 9 gives best compression output. Refer the below command.

$ bzip2 -9 myfile.txt   # 9 for Best Compression

Check the License & Package version of bzip2 Linux command

Use bzip2 command with argument -L to check the License & Package version of bz2 file compression tool.

$ bzip2 -L   # Check Version & License
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.
   
   Copyright (C) 1996-2010 by Julian Seward.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms set out in the LICENSE file, which is included
   in the bzip2-1.0.6 source distribution.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   LICENSE file for more details.

For more help and information on bzip2 Linux command you can use the below command.

$ man bzip2   # Access bzip2 manual page
Related Post