Signup/Sign In

Decompress and print files with bzcat command in Linux terminal

In this tutorial, we will learn about another Linux/Unix command line tool, bzcat. It is used compress and decompress 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.

How to install bzcat tool?

Debian based - apt install bzip2
Raspbian - apt-get install bzip2
Alpine - apk add bzip2
Arch Linux - pacman -S bzip2
CentOS - yum install bzip2
Fedora - dnf install bzip2
OS X - brew install bzip2
Docker - docker run cmd.cat/bzip2 bzip2

Using bzcat coimmand to decompress files in Linux terminal

More about bzcat command

bzcat expects a list of file (which are compressed) names to accompany the command-line flags/options. Each file is replaced by a decompressed version of itself. 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. File name handling is naive in the sense that there is no mechanism for preserving original file names, permissions, ownerships or dates in file systems which lack these concepts, or have serious file name length restrictions, such as MS-DOS systems.

Files which were not created by bzcat will be detected and ignored (based on hex values), and a warning issued. bzcat attempts to guess the filename for the decompressed file from that of the compressed file as follows:

filename.bz2 becomes filename
filename.bz becomes filename
filename.tbz2 becomes filename.tar
filename.tbz becomes filename.tar
anyothername becomes anyothername.out


Notice that if the provided compressed file does not have a file extension (bz2,bz.tbz.tbz2) then the compressed file will get ".out" appended to the original name.

Syntax of bzcat command:

bzcat [ -s ] [ filenames ...  ]
bzcat [ -h|--help ]

Options of bzcat command:

-c, --stdout You can compress or decompress files to the standard output by using this flag.
-d, --decompress The complement to -d: force decompression.
-z, --compress Force compression.
-t, --test Check integrity of the specified file(s), but don't decompress them. This really performs a trial decompression and throws away the result.
-f, --force bzcat will by default not overwrite existing files. We can force overwrite of output files. Normally, bzcat will not overwrite existing output files. Also forces bzcat to break hard links to files, which it otherwise wouldn't do. bzcat normally declines to decompress files which don't have the correct magic header bytes. If forced (-f), however, it will pass such files through unmodified.
-k, --keep Keep (don't delete) input files during compression or decompression.
-s, --small Reduce memory usage, for compression, decompression and testing. Files are decompressed and tested using a modified algorithm which only requires 2.5 bytes per block byte. This means any file can be decompressed in 2300 k of memory, albeit at about half the normal speed.
During compression, -s selects a block size of 200 k, which limits memory use to around the same figure, at the expense of your compression ratio. In short, if your machine is low on memory (8 megabytes or less), use -s for everything.
-q, --quiet Suppress non-essential warning messages. Messages pertaining to I/O errors and other critical events will not be suppressed.
-1 (or --fast) to -9 (or --best) Set the block size to 100 k, 200 k ... 900 k when compressing. Has no effect when decompressing. In particular, --fast doesn't make things significantly faster. And --best merely selects the default behavior.
-- Treats all subsequent arguments as file names, even if they start with a dash. This is so you can handle files with names beginning with a dash, for example: bzcat -- -myfilename.
-v, --verbose Verbose mode - show the compression ratio for each file processed. Further -v's increase the verbosity level, spewing out lots of information which is primarily of interest for diagnostic purposes.
-L, --license Display the software version, license, terms and conditions.
--help display this help and exit.

bzcat command example

We will use this file for further examples:

$ echo -e "This is a file containing plain text. \nWe will use this text file for bzip commands explanation" > foo
$
$ cat foo
This is a file containing plain text.
We will use this text file for bzcat commands explanation

1. Decompress data with bzcat command

One of the most straightforward use cases of the bzcat command is to decompress the provided data and write it to a file. Let’s take a look at the two ways to do so:
Read data from a file and write a decompressed form of that data to the same file and also remove .bz2 as extension. i.e, The file named foo.bz2 becomes foo after decompression.

$ ls
foo.bz2
$ bzcat foo
$ ls
foo

Read data from standard input (stdin) and decompress the data. So, let us again use bzcat to decompress data for a file named bar.

$ cat bar | bzcat
bzcat: I won't read compressed data from a terminal.
bzcat: For help, type: `bzcat --help'.


But, If no file names are specified, bzcat decompresses from standard input to standard output. In this case, bzcat will decline to write decompressed output to a terminal.

$ cat bar | bzcat > foo
$ ls
bar  foo
$ cat -A foo
This is a file containing plain text. \nWe will use this text file for bzcat commands explanation

Use -dc (or -d -c) options to read data from a file and decompress the data and write to standard output.

$ bzcat -zc foo.bz2 > foo
$ ls
foo  foo.bz2

2. Compress data using bzcat command

Read data from a file and write a compressed form of that data to the same file and also add .bz2 as extension. i.e, The file named foo becomes foo.bz2 after compression.

$ ls
foo
$ bzcat -c foo
$ ls
foo.bz2


Read data from standard input (stdin) and compress the data. So, let us again use bzcat to compress data for a file named foo.

$ echo -e "This is a file containing plain text. \nWe will use this text file for bzcat commands explanation" | bzcat -c
bzcat: I won't write compressed data to a terminal.
bzcat: For help, type: `bzcat --help'.


But, If no file names are specified, bzcat (with -z option) compresses from standard input to standard output. In this case, bzcat will decline to write compressed output to a terminal, as this would be entirely incomprehensible and therefore pointless.

$ echo -e "This is a file containing plain text. \nWe will use this text file for bzip commands explanation" | bzcat > bar
$ ls
bar  foo
$ cat -A bar
BZh91AY&SYM-QM-^KM-^?V^@^@^IM-SM-^@^@^P@^A^DM-^@?M-gM-^M-P ^@@M-SM"OSM-TM-rM-^^M-!M-dOBM-^@^@^C&F^]g9M-2M-sM-$M-0hM-?^YTV^QM-LM-aM-^J^\^&^KJM-kM-hM-jM-R1M->4+M-^AM-K_M-M-t^D?.lM-tM-\M-^WM-r7^CM-\M-ZM-^DiM-7M-XM-;M-^R)M-BM-^DM-^FM-^L_M-zM-0

Use -zc (or -z -c) options to read data from a file and compress the data and write to standard output.

$ bzcat -zc foo > foo.bz2
$ ls
foo  foo.bz2

Conclusion

In summary, the Linux bzcat command is a useful tool for decompressing and displaying the contents of a bzip2 compressed file. It can be used to quickly view the contents of a compressed file, or to pipe the decompressed output to other command line utilities for further processing.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.