BZ is Eazy! bzip2, bzgrep, bzcmp, bzdiff, bzcat, bzless, bzmore examples

by Sasikala on October 25, 2010

bzip2 command is used for compression and decompression of files. The main advantage of bzip2 is the best compression size. bzip2 vs gzip: bzip2 will compress better than gzip. Speed of bzip2 is somewhat slower than gzip and zip. bzip2 provides high rate of compression with reasonable speed.

There are several Linux bz commands available to manipulate the bzip2 files. This article explains various bz commands with 6 practical examples.

Example 1: Compressing a file using bzip2

When you compress a file using bzip2 command, it creates a compressed file with *.bz2 extension as shown below.

$ bzip2 trace

$ ls -l trace.bz2
-rw-r--r-- 1 root root       54167 Jan 23  2009 trace.bz2

Example 2: Search operation in bzip2 file using bzgrep

bzgrep command is used to search for a string or a pattern (regular expression) on bzip2 compressed files.

bzgrep will apply grep to data from files in the bzip2 format without requiring on-disk decompression. So all the options of a grep command will be applied for bzgrep also.

Please refer our article Get a Grip on the Grep! for grep examples.

Syntax:
bzgrep grep-options -e pattern filename

In the below example, trace.bz2 is a compressed trace file which is of size 58M.

$ bzgrep -i "CONSOLE=.*"  trace.bz2
2010-10-11T08:40:28.100 gs(16985): CONSOLE=/dev/pts/0
2010-10-11T08:40:29.772 gs(17031): CONSOLE=/dev/pts/0
2010-10-11T08:40:58.140 gs(17099): CONSOLE=/dev/pts/0
2010-10-11T08:41:27.547 gs(17164): CONSOLE=/dev/pts/0
2010-10-11T08:41:57.962 gs(17233): CONSOLE=/dev/pts/0
2010-10-11T08:42:28.392 gs(17294): CONSOLE=/dev/pts/0
2010-10-11T08:42:57.721 gs(17439): CONSOLE=/dev/pts/0

If bzgrep is not there, you have to decompress the file manually and do a grep on that, where bzgrep does this process internally and gives you the required output. bzegrep and bzfgrep commands will apply egrep and freg operation on bzip2 files respectively.

Example 3: View the bzip2 file using bzcat

If you want only to read the compressed file without decompressing it, use the bzcat command as shown below.

$ bzcat trace.bz2
0: ERR: Wed Sep 22 09:59:42 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
0: ERR: Wed Sep 22 09:59:47 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
Retry. (OpC51-22)
Database: ORA-01653: unable to extend table OPC_OP.OPC_HIST_MESSAGES by 64 in tablespace OPC_6
(OpC50-15)
.
.

bzcat command displays the uncompressed content into standard output file for the users to view the content.

Example 4. Paging bzip2 compressed file with bzless & bzmore

bzless and bzmore command allows you to view the content of bzip2 compressed files page by page. bzmore works on files compressed with bzip2 and also on uncompressed files.

Please refer our previous article The Ultimate Power of Less to know the power the less command.

$ bzless trace.bz2

$ bzmore trace.bz2

0: ERR: Wed Sep 22 09:59:42 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
0: ERR: Wed Sep 22 09:59:47 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
Retry. (OpC51-22)
Database: ORA-01653: unable to extend table OPC_OP.OPC_HIST_MESSAGES by 64 in tablespace OPC_6
(OpC50-15)
.
.
--More--

Example 5. Compare bzip2 files using bzcmp

You can compare two bzip2 compressed file using bzcmp command. It internally uses cmp command to compare the content of the compressed contents. Here you can see the output of comparison of the two normal files and compressed files.

$ cmp System.txt.001 System.txt.002
System.txt.001 System.txt.002 differ: byte 20, line 2

$ bzcmp System.txt.001.bz2 System.txt.002.bz2
- /tmp/bzdiff.csgqG32029 differ: byte 20, line 2

Example 6. Find the difference of two bzip2 files using bzdiff

In Linux, diff command will compare two files and give you the lowdown on just how different they are. If you give bz2 files to diff command, it will not be in a position to explain the difference.

For bzip2 compressed files, bzdiff command gives the differences of two bzip2 compressed files as shown below.

$ bzdiff System.txt.001.bz2 System.txt.002.bz2
2c2
< 0: ERR: Mon Sep 27 12:19:34 2010: gs(11153/1105824064): [chk_sqlcode.scp:92]: Database: ORA-01654: unable to extend index OPC_OP.OPCX
_ANNO_NUM by 64 in tablespace OPC_INDEX1
---
> 0: ERR: Wed Sep 22 09:59:42 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.
OPC_HIST_MESSAGES by 64 in tablespace OPC_6
4,5c4
< Retry. (OpC51-22)
< Database: ORA-01654: unable to extend index OPC_OP.OPCX_ANNO_NUM by 64 in tablespace OPC_INDEX1
---
> 0: ERR: Wed Sep 22 09:59:47 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.
OPC_HIST_MESSAGES by 64 in tablespace OPC_6

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 Rick Stanley October 25, 2010 at 8:13 am

“bzip2 will compress better than gzip.”

Are there any stats that compare the compression ratios between gzip and bzip2 compressing various types of files, and/or various options of both?

I was aware of bzip2, but not of the other related apps.

Thanks!

2 DarkForce October 25, 2010 at 12:59 pm

Good stuff….thanks

3 reddy October 25, 2010 at 11:07 pm

how to compress entire folder

4 Mike Stewart October 29, 2010 at 3:43 pm

I recommend LZMA or XZ compression generally if you’re looking for an improvement over gzip.

here’s a comparison of lossless formats like zip (deflate), bzip (successor to deflate), LZMA (7-zip & XZ), etc.

http://en.wikipedia.org/wiki/Lossless_data_compression#General_purpose

Leave a Comment

Previous post:

Next post: