Introduction to bzip2 command
File compression is a technique in which the file or group of files is compressed into a single archive file to reduce the size. There are several tools that you can use to compress files in Linux such as gzip, 7zip, tar, bzip2, etc.
bzip2 is one of the popular compression tools for Linux. It uses the Burrows-Wheeler block sorting text compression algorithm and Huffman coding. It can only compress files, not directories.
Syntax to use bzip2 command
The bzip2 command is simple to use. The syntax is as follows:
$ bzip2 [option] filename
Some important options in bzip2 are:
- -d: To decompress files
- -t: To test the integrity of a compressed file
- -f: Forcefully overwrite existing files
- -k: Keep original input file after compression or decompression
If no filenames are specified, bzip2 tries to compress from standard input to standard output. bzip2 declines to write compressed output to a terminal.
golinux@ubuntu-PC:~$ bzip2
bzip2: I won't write compressed data to a terminal.
bzip2: For help, type: `bzip2 --help'.
Different examples to use bzip2 command
1. bzip2 command to compress a file
You can specify a file name after bzip2 command to compress a specified file. The original file will be replaced by the compressed version having the file name "original_name.bz2".
$ bzip2 test.txt
Sample Output:
2. bzip2 command to compress multiple files
To compress multiple files, you will have to specify multiple files in a single bzip2 command.
$ bzip2 test1.txt test2.txt test3.txt
Sample Output:
You can use wildcard (*) to compress all files. The *.txt
compresses all text files in the directory.
$ bzip2 *.txt
Sample Output:
3. bzip2 command to decompress a file
You can decompress or extract a .bz2
files using the -d
or --decompress
option.
$ bzip2 -d test.txt.bz2
OR
$ bzip2 --decompress test.txt.bz2
Sample Output:
You can specify multiple .bz2
files to extract them all.
$ bzip2 -d test1.txt.bz2 test2.txt.bz2 test3.txt.bz2
Sample Output:
4. Compress a file forcefully using bzip2 command
The -z
or --compress
option forces compression of a file, regardless of the invocation name.
$ bzip2 -z test.txt
OR
$ bzip2 --compress test.txt
5. Keep original files after compression or decompression
You might have noticed that the input files are replaced by the compressed or decompressed version of the file. You can change this behavior and opt to keep original files using the -k
or --keep
option.
$ bzip2 -k test.txt
OR
$ bzip2 --keep test.txt
Sample Output:
6. Check integrity of the compressed file
You can test the integrity of the compressed file using the -t
or --test
option. It is helpful to know whether the specified file is a valid bzip2 file or not.
$ bzip2 -t test.txt.bz2
OR
$ bzip2 --test test.txt.bz2
Sample Output:
golinux@ubuntu-PC:~$ bzip2 -tv test.txt.bz2
test.txt.bz2: ok
Suppose, you created a bzip2 file using the cat command. If you test the integrity of that file, you will get the following errors.
7. Force overwrite of output files
Normally, bzip2 does not overwrite the existing file. The -f
or --force
option forces the bzip command to overwrite the existing file with the output file. It also forces bzip2 to break hard links to files.
$ bzip2 -f test.txt
OR
$ bzip2 --force test.txt
Sample Output:
8. Compress or decompress to standard output
The -c
or -stdout
option is used to compress or decompress the file to standard output.
$ bzip2 -c filename
OR
$ bzip2 --stdout filename
Sample Output:
When decompressing, it shows the content of a file.
You can also use the bzcat command to display the content of a compressed file.
golinux@ubuntu-PC:~$ bzcat test.txt.bz2
This is a test file.
9. bzip2 command to add compression level
When compressing, bzip2 command allows you to specify the compression level ranging from -1
(or --fast
) to -9
(or --best
). It has no effect when decompressing. By default, bzip2 uses the -6
compression level to compress files.
The following command uses the -1
or --fast
compression level which has the fastest compression speed with a lesser compression ratio.
$ bzip2 -1 test.txt
OR
$ bzip2 --fast test.txt
The compression level -9
or --best
has the lowest compression speed with a maximum compression ratio.
$ bzip2 -9 test.txt
OR
$ bzip2 --best test.txt
10. Display the verbose output of a command
The -v
or --verbose
turns the verbose mode on and displays the compression ratio for each file processed.
$ bzip2 -v test.txt
OR
$ bzip2 --verbose test.txt
Sample Output:
Using multiple v
increases the verbosity level resulting in more information that can be used for diagnostic purposes.
11. Suppress warning messages in the output
The -q
or --quiet
option suppresses the non-essential warning messages in the output. It does not hide the warnings related to I/O errors and other critical events.
$ bzip2 -q filename
OR
$ bzip2 --quiet filename
Sample Output:
12. Reduce memory usage
The -s
or --small
option reduces the memory usage, for compression, decompression, and testing. It uses a modified algorithm that only requires 2.5 bytes per block byte to decompress and test a file.
$ bzip2 -s filename
OR
$ bzip2 --small filename
Conclusion
This tutorial teaches you how to compress files using bzip2 command in Linux. We hope this article helps you to understand the bzip2 tool and its features. If you still have any confusion, please let us know in the comment section below.
What's Next
15+ tar command examples in Linux [Cheat Sheet]
20 grep command examples in Linux [Cheat Sheet]
Further Reading