Linux bzip2: Compress & Decompress .bz2 Files
In this article
Yes, Linux uses the bzip2 command to compress and decompress .bz2 files. You compress with bzip2 filename and decompress with bunzip2 filename.bz2 (or bzip2 -d). This article explains the command's syntax, options, practical examples, and how it compares to other compression tools.
What is bzip2 and Why Use It?
bzip2 is a free and open-source compression program that uses the Burrows-Wheeler algorithm followed by Huffman coding. It typically achieves better compression ratios than the older gzip (which uses DEFLATE), especially on text files, source code, and log files. The trade-off is that bzip2 is generally slower than gzip and requires more memory, but for archiving large datasets where disk space is the priority, bzip2 remains a solid choice.
The .bz2 extension is standard for files compressed with bzip2. Unlike tar (which bundles multiple files into one archive), bzip2 compresses a single file. To compress multiple files or directories, you first create a tar archive and then compress that with bzip2, resulting in a .tar.bz2 file (often called a tarball).
Basic bzip2 Commands
The core usage is straightforward. Here are the most common commands:
- Compress a file:
bzip2 file.txt— this createsfile.txt.bz2and deletes the originalfile.txt. - Decompress a file:
bunzip2 file.txt.bz2— this restoresfile.txtand removes the.bz2file. - Decompress with bzip2 directly:
bzip2 -d file.txt.bz2— same asbunzip2. - Compress but keep the original file:
bzip2 -k file.txt— the-k(keep) flag preserves the input file. - Decompress but keep the .bz2 file:
bunzip2 -k file.txt.bz2— keep the compressed file after decompressing. - Compress to stdout (for piping):
bzip2 -c file.txt > file.txt.bz2— write compressed data to standard output, allowing you to redirect it anywhere.
Notice that by default, bzip2 removes the original file after successful compression/decompression. This is a common point of surprise for new users, so the -k flag is handy when you need both versions.
Working with tar and bzip2 Together
In practice, you rarely compress a single file. You usually archive a directory tree with tar and then compress the result. The most common commands are:
- Create a compressed archive:
tar -cjf archive.tar.bz2 /path/to/directory— the-jflag tells tar to use bzip2 compression. - Extract a compressed archive:
tar -xjf archive.tar.bz2— extract files from the tarball, decompressing on the fly. - List contents without extracting:
tar -tjf archive.tar.bz2— show what's inside the archive.
You can also use the older style flags: tar -cjf and tar -xjf are equivalent to tar --bzip2 -cf and tar --bzip2 -xf. The -j option is widely supported across Linux distributions and macOS.
Compression Levels and Performance
bzip2 offers compression levels from 1 (fastest, least compression) to 9 (slowest, best compression). The default is level 9, which is why bzip2 can feel slow on large files. You can specify a level with -1 through -9:
bzip2 -1 file.txt— fastest, lowest compression ratio.bzip2 -9 file.txt— slowest, highest compression ratio (default).
For very large files, you might experiment with -5 or -6 to balance speed and size. The difference between level 9 and level 1 is often only a few percent in file size, but the time difference can be significant. On modern multi-core systems, bzip2 is single-threaded; it uses only one CPU core. If you need parallel compression, look at pbzip2 (parallel bzip2) which can use multiple cores, although it's not installed by default on all distributions.
Common Errors and Troubleshooting
Here are a few issues you might run into and how to resolve them:
- "bzip2: Cannot open: No such file or directory" — You mistyped the filename or the file is in a different directory. Use
lsto verify the exact name. - "bzip2: Compressed file ends unexpectedly" — The
.bz2file is corrupted or incomplete. Try re-downloading or re-transferring it. If you have the original, re-compress it. - "bzip2: Data integrity error when decompressing" — The file is corrupted. This can happen after a bad download or disk error. Check the file's checksum if one was provided.
- Out of memory errors — bzip2 requires memory proportional to the block size (default 900KB). For extremely large files, you might need to reduce the block size with
-1(uses 100KB blocks) or upgrade your system's RAM. - Permission denied — You don't have write permission in the current directory. Use
sudoor change to a directory you own.
If a decompression fails, you can sometimes recover partial data with bzip2recover, a utility that attempts to salvage data from damaged .bz2 files. It's not perfect, but it can help in emergencies.
bzip2 vs gzip vs xz
Linux offers several compression tools. Here's a quick comparison to help you choose:
- gzip — Fast, widely used, and good for general purposes. Compression ratio is lower than bzip2 but speed is much higher. Files end with
.gz. - bzip2 — Better compression than gzip, slower, and uses more memory. Files end with
.bz2. Good for text-heavy archives where size matters more than speed. - xz — The modern favorite. It uses LZMA2 and typically achieves the best compression ratio among the three, but is also the slowest. Files end with
.xz. Many Linux distributions now shiptar.xzarchives for their packages.
For a quick rule of thumb: if you need speed, use gzip. If you need the smallest file and are willing to wait, use xz. bzip2 sits in the middle, offering a good balance for many use cases, especially when you need compatibility with older systems where xz may not be installed.
Frequently Asked Questions
How do I compress multiple files into one .bz2 file?
bzip2 only compresses a single file. To compress multiple files or a directory, first create a tar archive and then compress it: tar -cjf archive.tar.bz2 file1 file2 dir1. This bundles everything into one tarball and compresses it with bzip2.
Why does bzip2 delete my original file?
This is the default behavior. The developers designed it that way to prevent accidental duplication. If you want to keep the original, use the -k (keep) flag: bzip2 -k file.txt.
How do I decompress a .tar.bz2 file?
Use tar -xjf archive.tar.bz2. This extracts the contents into the current directory. If you want to extract to a specific directory, add -C /path/to/dir.
Can I use bzip2 on Windows or macOS?
Yes. macOS includes bzip2 by default in the terminal. On Windows, you can use tools like 7-Zip to open .bz2 files, or install Windows Subsystem for Linux (WSL) to use the same commands as Linux.
What is the difference between .bz2 and .bzip2?
There is no difference. Both extensions are used for files compressed with bzip2. .bz2 is the standard and most common, while .bzip2 is occasionally seen but is less standard.
Is bzip2 still maintained?
Yes, bzip2 is still maintained, though development is slow. The original author, Julian Seward, stepped back, and the project is now maintained by a small group. It remains a stable and reliable tool, though newer alternatives like xz and zstd are gaining popularity.