lbzip2 is a parallel bzip2 utility. bzip2 is a single-threaded, whereas lbzip2 employs multiple threads that result in better performance on multi-core systems. lbzip2 also uses the Burrows-Wheeler block-sorting text compression algorithm. The files decompressed by lbzip2 have .bz2
extensions like bzip2. lbzip2 uses multiple threads when decompressing .bz2
files created by standard bzip2 too.
How to install lbzip2
In most Linux systems, lbzip2 tool is not installed by default. You can use the below commands to install lbzip2 according to your Linux distribution.
To install lbzip2 on CentOS, Fedora and RHEL
$ sudo yum install lbzip2
To install lbzip2 on Ubuntu and Debian
$ sudo apt install lbzip2
Syntax to use lbzip2 command
lbzip2 [options] [files...]
Most of the options are similar to the bzip2 command.
-1
to-9
: Set the block size to100k
to900k
(default is9
).-d
,--decompress
: Decompress the specified files.-c
,--stdout
: Compress or decompress to standard output.-f
,--force
: Force overwrite of output file and compress links.-k
,--keep
: Keep (don't delete) input files during compression or decompression.-t
,--test
: Check the integrity of the specified files.-n
,--no-stdin
: Do not read from standard input.-v
,--verbose
: Verbose mode, display compression ratio for each file.-q
,--quiet
: Suppress non-essential error messages.-S
,--suffix=SUF
: Use suffixSUF
instead of.bz2
.-h
,--help
: Display a help message and exit.-V
,--version
: Display version information and exit.
Some Examples:Â
# Compress a file
lbzip2 file.txt
# Decompress a file
lbzip2 -d file.txt.bz2
# Compress a file with a specific block size
lbzip2 -1 file.txt
# Decompress a file to standard output
lbzip2 -dc file.txt.bz2 > file.txt
# Compress a file and keep the original
lbzip2 -k file.txt
# Compress multiple files
lbzip2 file1.txt file2.txt file3.txt
# Test the integrity of a compressed file
lbzip2 -t file.txt.bz2
# Display help message
lbzip2 -h
# Display version information
lbzip2 -V
1. lbzip2 command to compress a file
You can simply specify a file name after lbzip2 command to compress that file. The original file will be replaced by the compressed version of it.
$ lbzip2 filename
Sample Output:
-z
or --compress
options can also be used to compress a file.
$ lbzip2 -z filename
OR
$ lbzip2 --compress filename
2. lbzip2 command to compress multiple files
lbzip2 command takes multiple file names as arguments. So, you can compress multiple files with a single lbzip2 command.
$ lbzip2 file1 file2 file3
Sample Output:
3. lbzip2 command to decompress a file
You can use -d
or --decompress
option to decompress a .bz2
file.
$ lbzip2 -d file.bz2
OR
$ lbzip2 --decompress file.bz2
Sample Output:
4. Keep input files after successful compression or decompression
By default, lbzip2 command removes the input file after compression or decompression. You can use -k
or --keep
option to not remove files after any operation.
$ lbzip2 -k filename
OR
$ lbzip2 --keep filename
Sample Output:
As you can see, test.txt is not replaced this time.
5. Set the compression block size during compression
lbzip2 compresses large files in blocks. It can operate at various block sizes, ranging from 100k to 900k in 100k steps, and it allocates only as much memory as it needs to. The block size affects both the compression ratio achieved, and the amount of memory needed both for compression and decompression.
The options -1
to -9
set the compression block size to 100K to 900K, in increments of 100K. For example, -1
will set the compression block size to 100K, -2
will set 200K, -3
will set 300K, and so on. It is ignored during decompression.
$ lbzip2 -1 filename
OR
$ lbzip2 --fast filename
--fast
is an alias for -1
whereas --best
is alias for -9
.
6. Force overwrite existing file
If you want lbzip to overwrite existing files, you have to use -f
or --force
option.
$ lbzip2 -f filename
OR
$ lbzip2 --force filename
Sample Output:
7. Print more detailed information on progress
Normally, lbzip2 command does not print any output on the successful compression or decompression. You can view more detailed information about compression or decompression progress using -v
or --verbose
option.
$ lbzip2 -v filename
OR
$ lbzip2 --verbose filename
Sample Output:
8. Write output to standard output
With option -c
or --stdout
, lbzip2 writes output to standard output, even when FILE operands are present. When decompressing, it shows the content of a file in the output.
$ lbzip2 -c filename
OR
$ lbzip2 --stdout filename
Sample Output:
golinux@ubuntu-PC:~$ lbzip2 -cd test.txt.bz2
This is a test file.
9. Set the number of compressor/decompressor threads
The -n
option is used to set the number of compressor or decompressor threads. The number should be a positive integer.
$ lbzip2 -n NUM
Sample Output:
golinux@ubuntu-PC:~$ lbzip2 -n 5 test.txt
10. Test decompression
The -t
or --test
option helps to test the decompression. It discards output instead of writing it to files or standard output. -v
can be used for verbose information.
$ lbzip2 -t filename
OR
$ lbzip2 --test filename
Sample Output:
This command is useful to check whether the specified file is a valid .bz2
file or not.
For example, if you create a .bz2
file using cat or similar commands, you will get an error message when you test that file.
Conclusion
This tutorial covers the most common practical examples of lbzip2 commands. We hope this tutorial helps you to understand how to use lbzip2 commands in Linux. If you have any confusion, do let us know in the comment section below.
What's Next
10+ bzip2 command examples in Linux [Cheat Sheet]
10+ xz command examples in Linux [Cheat Sheet]
Further Reading