Linux bzip2 Command: Compress a File in bzip2 Format

bzip2 compresses individual files into the .bz2 format. By default, it replaces the original file with the compressed file, and it does not have a directory bundling feature.

bzip2 creates a separate compressed result for each file. Use -k to keep the original, and -c to write the output stream to another file. To bundle multiple files and directories into one file, combine it with tar like tar -cjf.

Basic syntax

bzip2 [options] [files...]

Installed implementations and options may vary depending on the distribution. Check the description on the current system with man bzip2.

Examples

Compress while preserving the original

Creates data.csv.bz2 and keeps data.csv as well.

bzip2 -k data.csv

Compress to a different file name

This is the method that writes to standard output. Make sure that the target file does not already exist.

bzip2 -c data.csv > data-copy.csv.bz2

Check compressed file

Checks the integrity without actually unpacking the file.

bzip2 -t data.csv.bz2

Checking bzip2 Compressed Files

bzip2 compresses a single file into the .bz2 format and by default replaces the original. To keep the original, you can send the compressed output to standard output and save it to a new file.

bzip2 -c note.txt > note.txt.bz2
bzip2 -t note.txt.bz2
ls note.txt*

Example file list:

note.txt  note.txt.bz2

bzip2 -t usually finishes quietly if the structure is normal. Simply having the file does not guarantee that the original can be fully restored, so important data requires actual recovery checks and separate backups. To combine multiple files into one, use it together with tar.

Main Options and Format

Options/Format Description
-k Keeps the input file.
-c Sends the result to standard output.
-t Checks the compressed data.
-1 ... -9 Selects the compressed block size.
-v Displays detailed processing information.

Precautions when using

The default compression deletes the original, so if preservation is needed, specify -k. bzip2 does not overwrite existing output files by default, but -f can force it. Compression is not encryption, and very small files may actually become larger after compression.

Frequently Asked Questions

Can the directory be compressed with bzip2 data/?

bzip2 does not directly combine directories into a single file. Use tar like tar -cjf data.tar.bz2 data/.

Official Documentation

You can check the exact behavior of the options and implementation differences in the official bzip2 documentation.