Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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.

[email protected]:~/data$ bzip2 myfile.txt   # Compress a File

# Output

[email protected]:~/data$ ls
myfile.txt.bz2

Compress a file with a Standard Output

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

[email protected]:~/data$ bzip2 -c myfile.txt > myfile.txt.bz2  # Compress a file with standard output

# Output

[email protected]:~/data$ 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.

[email protected]:~/data$ bzip2 -k myfile.txt   # Compress file without delete Input file

# Output

[email protected]:~/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.

[email protected]:~/data$ bzip2 myfile.txt myfile1.txt myfile2.txt myfile3.txt   # Compress multiple files at once
 
# Output

[email protected]:~/data$ ls
myfile1.txt.bz2  myfile2.txt.bz2  myfile3.txt.bz2  myfile.txt.bz2

Also Read :

  • BEST ZIP COMMAND WITH EXAMPLES IN LINUX
  • HOW TO INSTALL 7ZIP (7Z) ARCHIVE TOOL IN UBUNTU 16.04
  • BEST LINUX GZIP COMMAND (GZIP COMPRESSION) WITH EXAMPLES
  • HOW TO INSTALL XARCHIVER ARCHIVE MANAGER IN UBUNTU 16.04 – A BEST GNOME ARCHIVE MANAGER FOR LINUX

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.

[email protected]:~/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 error. To prove so let’s create a bzip2 file using touch command and then check for integrity.

[email protected]:~/data$ touch myfile.txt.bz2

[email protected]:~/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 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.

[email protected]:~/data$ bzip2 -d myfile.txt.bz2   # Uncompress a file

# Output

[email protected]:~/data$ ls
myfile.txt

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

[email protected]:~/data$ bunzip2 myfile.txt.bz2 

# Output

[email protected]:~/data$ 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.

[email protected]:~/data$ bzcat myfile.txt.bz2   # Check content of a Compressed file
Welcome to elinuxbook.com

Compress a file Forcefully

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

[email protected]:~/data$ bzip2 -f myfile.txt    # Compress a file forcefully

[email protected]:~/data$ 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.

[email protected]:~/data$ tar -cjvf data.tar.bz2 data/   # Compress a Directory
data/
data/myfile3.txt
data/myfile2.txt
data/myfile1.txt

# Output

[email protected]:~/data$ 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.

[email protected]:~/data$ bzip2 -1 myfile.txt   # 1 for Fast Compression

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

[email protected]:~/data$ 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.

[email protected]:~/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.

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

[email protected]:~/data$ man bzip2   # Access bzip2 manual page

Here I have included all possible command of bz2 (bzip2 Linux command) and unzip bz2 file commands. If you found this article useful then Like us, Share this post on your preferred Social media, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.

The post COMPRESS FILES USING BZ2 (BZIP2 LINUX COMMAND) AND UNZIP BZ2 FILE IN LINUX appeared first on Elinuxbook: Linux Tutorials, Guides, Howtos, Tips and Tricks.



This post first appeared on Elinuxbook: Linux Tutorials, Guides, Howtos, Tips And Tricks, please read the originial post: here

Share the post

COMPRESS FILES USING BZ2 (BZIP2 LINUX COMMAND) AND UNZIP BZ2 FILE IN LINUX

×

Subscribe to Elinuxbook: Linux Tutorials, Guides, Howtos, Tips And Tricks

Get updates delivered right to your inbox!

Thank you for your subscription

×