Compress Files Using Bz2 (Bzip2 Linux Command) And Unzip Bz2 File In Linux

In this article, we are going to learn how to compress files using the bz2 file compression tool (bzip2 Linux command) and unzip bz2 compressed file. bzip2 is an open-source compress tool similar to the zip & gzip compression tool used to compress large sized files to reduce their 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 in the year 1996 and released under a 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 than 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 the bz2 file compression tool include:

  • It’s a cross-platform application available for major operating systems i.e. Linux, Microsoft Windows, macOS.
  • Can recover data from corrupted bz2 file.
  • Available for both 32 bit and 64-bit operating systems.
  • It can create the fast and best compression of the files.
Compress Files Using Bz2 (Bzip2 Linux Command) And Unzip Bz2 File In Linux
Compress Files Using Bz2 (Bzip2 Linux Command) And Unzip Bz2 File In Linux

Compress Files Using Bz2 (Bzip2 Linux Command) And Unzip Bz2 File In Linux:

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

Compress a File

Compress a file using the bzip2 Linux command.

itsmarttricks@ubuntu:~/data$ bzip2 myfile.txt   # Compress a File

# Output

itsmarttricks@ubuntu:~/data$ ls
myfile.txt.bz2

Compress a file with a Standard Output

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

itsmarttricks@ubuntu:~/data$ bzip2 -c myfile.txt > myfile.txt.bz2  # Compress a file with standard output

# Output

itsmarttricks@ubuntu:~/data$ ls
myfile.txt  myfile.txt.bz2

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

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

itsmarttricks@ubuntu:~/data$ bzip2 -k myfile.txt   # Compress file without delete Input file

# Output

itsmarttricks@ubuntu:~/data$ 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.

itsmarttricks@ubuntu:~/data$ bzip2 myfile.txt myfile1.txt myfile2.txt myfile3.txt   # Compress multiple files at once
 
# Output

itsmarttricks@ubuntu:~/data$ ls
myfile1.txt.bz2  myfile2.txt.bz2  myfile3.txt.bz2  myfile.txt.bz2

Also Read : Best Linux Gzip Command (Gzip Compression) With Examples

Check integrity of a specified file

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

itsmarttricks@ubuntu:~/data$ 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 an error. To prove so let’s create a bzip2 file using the touch command and then check for integrity.

itsmarttricks@ubuntu:~/data$ touch myfile.txt.bz2

itsmarttricks@ubuntu:~/data$ 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 create this file using the 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.

itsmarttricks@ubuntu:~/data$ bzip2 -d myfile.txt.bz2   # Uncompress a file

# Output

itsmarttricks@ubuntu:~/data$ ls
myfile.txt

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

itsmarttricks@ubuntu:~/data$ bunzip2 myfile.txt.bz2 

# Output

itsmarttricks@ubuntu:~/data$ ls
myfile.txt

Check the content of a bz2 compressed file using bzcat command

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

itsmarttricks@ubuntu:~/data$ bzcat myfile.txt.bz2   # Check content of a Compressed file
Welcome to itsmarttricks.com

Compress a file Forcefully

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

itsmarttricks@ubuntu:~/data$ bzip2 -f myfile.txt    # Compress a file forcefully

itsmarttricks@ubuntu:~/data$ ls
myfile.txt.bz2

Also Read : TAR Command Examples in Linux

Compress a directory with the bz2 file compression tool

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

itsmarttricks@ubuntu:~/data$ tar -cjvf data.tar.bz2 data/   # Compress a Directory
data/
data/myfile3.txt
data/myfile2.txt
data/myfile1.txt

# Output

itsmarttricks@ubuntu:~/data$ ls
data  data.tar.bz2

Here argument -j is for bzip2 compression.

Level’s of Compression

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

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

itsmarttricks@ubuntu:~/data$ bzip2 -1 myfile.txt   # 1 for Fast Compression

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

itsmarttricks@ubuntu:~/data$ bzip2 -9 myfile.txt   # 9 for Best Compression

Check the License & Package version of the bzip2 Linux command

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

itsmarttricks@ubuntu:~/data$ 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.

Also Read : How To Install 7Zip (7z) Archive Tool In Ubuntu

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

itsmarttricks@ubuntu:~/data$ man bzip2   # Access bzip2 manual page

That’s all, In this article, we have explained the Compress Files Using Bz2 (Bzip2 Linux Command) And Unzip Bz2 File In Linux. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.