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

Quick Answer: To bzip2 a file, you typically use the `bzip2` command-line utility. Simply type `bzip2 your_file.txt` in your terminal to compress the file, which will create `your_file.txt.bz2` and delete the original. To decompress, use `bunzip2 your_file.txt.bz2`.

Key Facts

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:

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:

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:

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:

However, consider using gzip if:

Understanding Bzip2 Options

The `bzip2` command has several useful options:

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

Sources

  1. Bzip2 - WikipediaCC-BY-SA-4.0
  2. bzip2(1) - Linux man pagefair-use
  3. 7-Zipfair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.