How to bzip2 a file
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- Bzip2 uses the Burrows-Wheeler transform for compression, which is generally more effective than the DEFLATE algorithm used by gzip.
- Bzip2 achieves higher compression ratios than gzip for many types of data, especially text files.
- Compression and decompression with bzip2 can be slower than with gzip.
- The original file is deleted by default after compression with `bzip2`.
- Bzip2 is a free and open-source software, part of the GNU project.
What is Bzip2?
Bzip2 is a free and open-source lossless data compression utility developed by Julian Seward. It is designed to provide better compression ratios than its predecessor, gzip, by employing the Burrows-Wheeler transform algorithm. While it might take longer to compress and decompress files compared to gzip, the resulting smaller file sizes can be beneficial for storage and transmission.
How to Use Bzip2 on Linux/macOS
The primary way to use bzip2 is through the command line. Most Linux distributions and macOS come with the `bzip2` command pre-installed. Here are the basic commands:
Compressing a File
To compress a file, navigate to the directory where the file is located in your terminal and type the following command:
bzip2 your_file.txt
This command will:
- Compress `your_file.txt`.
- Create a new file named `your_file.txt.bz2`.
- Delete the original `your_file.txt` by default.
If you wish to keep the original file, you can use the -k or --keep option:
bzip2 -k your_file.txt
This will create `your_file.txt.bz2` and leave `your_file.txt` intact.
Decompressing a File
To decompress a file that has been compressed with bzip2, use the `bunzip2` command (which is essentially a link to `bzip2 -d`):
bunzip2 your_file.txt.bz2
This command will:
- Decompress `your_file.txt.bz2`.
- Create the original file `your_file.txt`.
- Delete the compressed file `your_file.txt.bz2` by default.
To keep the compressed file after decompression, use the -k option:
bunzip2 -k your_file.txt.bz2
Viewing Compressed File Contents (without decompressing)
Sometimes you might want to see the contents of a bzip2 compressed file without actually decompressing it. You can use the -c (stdout) option with `bzip2` or `bunzip2` and pipe the output to a viewer like `less`:
bzip2 -dc your_file.txt.bz2 | less
or
bunzip2 -dc your_file.txt.bz2 | less
Compressing Multiple Files
Bzip2 is designed to compress a single file at a time. If you need to compress multiple files, it's common practice to first archive them into a single file using `tar`, and then compress the resulting `.tar` file with `bzip2`. For example:
tar -cvf archive.tar file1.txt file2.log directory/
bzip2 archive.tar
This will create `archive.tar.bz2`.
The equivalent command to create a `.tar.bz2` archive directly is:
tar -cjvf archive.tar.bz2 file1.txt file2.log directory/
The -j option tells `tar` to use bzip2 for compression.
How to Use Bzip2 on Windows
Windows does not have `bzip2` built-in like Linux or macOS. You will need to download and install a third-party tool. Popular options include:
- 7-Zip: A free and open-source file archiver that supports bzip2 compression and decompression. You can download it from [www.7-zip.org](http://www.7-zip.org/). After installation, you can right-click files in Windows Explorer, go to the 7-Zip context menu, and choose 'Add to archive...' or 'Extract here...'.
- WinRAR: A popular shareware file archiver that also supports bzip2 format.
- Bzip2 for Windows ports: Various unofficial ports of the command-line `bzip2` utility are available online. You would typically need to download the executable and run it from the command prompt (cmd.exe or PowerShell).
Using a GUI tool like 7-Zip is generally the easiest method for Windows users.
When to Use Bzip2
Bzip2 is a good choice when:
- Maximum compression is a priority: If you need to save as much disk space as possible, bzip2 often outperforms gzip.
- Transferring large files over slow connections: Smaller file sizes mean faster transfers.
- Archiving data for long-term storage: High compression ratios ensure efficient use of storage space.
However, consider using gzip if:
- Speed is more important than compression ratio: gzip is generally faster at both compressing and decompressing.
- Compatibility is key: gzip is more universally supported out-of-the-box on many systems.
Understanding Bzip2 Options
The `bzip2` command has several useful options:
-d,--decompress: Forces decompression.-z,--compress: Forces compression (this is the default action).-k,--keep: Keeps input files after processing.-c,--stdout: Writes output to standard output; keeps original files unchanged.-f,--force: Overwrites an existing output file.-t,--test: Tests the integrity of a compressed file.-q,--quiet: Suppresses some warning messages.-v,--verbose: Produces verbose output, showing compression statistics.-1to-9: Selects the block size, affecting compression speed and ratio.-9provides the best compression but is the slowest, while-1is the fastest but offers the least compression. The default is-9.
For example, to test a file for integrity:
bzip2 -t your_file.txt.bz2
To compress using a smaller block size for faster compression:
bzip2 -1 your_file.txt
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Bzip2 - WikipediaCC-BY-SA-4.0
- bzip2(1) - Linux man pagefair-use
- 7-Zipfair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.