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.

What is the bzip2 command?

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.

More in This Category
Linux htop Command: Explore Processes Interactively

Linux htop Command: Explore Processes Interactively

Learn how to explore Processes Interactively with the Linux htop command, including practical examples, key options, and important precautions.

Essential Linux Commands: A Practical Reference by Task

Essential Linux Commands: A Practical Reference by Task

Browse essential Linux commands by task, including help, files, text processing, permissions, processes, networking, packages, storage, and system administration.

Linux shred Command: Overwrite File Data and Understand Its Limits

Linux shred Command: Overwrite File Data and Understand Its Limits

Learn how Linux shred overwrites file data, which options control its passes and removal, and why SSDs, snapshots, backups, and modern filesystems limit its guarantees.

Linux tee Command: Display and Save Pipeline Output

Linux tee Command: Display and Save Pipeline Output

Learn how to display and Save Pipeline Output with the Linux tee command, including practical examples, key options, and important precautions.

Linux dnf Command: Manage RPM Distribution Packages

Linux dnf Command: Manage RPM Distribution Packages

Learn how to manage RPM Distribution Packages with the Linux dnf command, including practical examples, key options, and important precautions.

Linux dig Command: Query DNS Records in Detail

Linux dig Command: Query DNS Records in Detail

Learn how to query DNS Records in Detail with the Linux dig command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux whereis Command: Locate Binaries, Source, and Man Pages

Linux whereis Command: Locate Binaries, Source, and Man Pages

Learn how Linux whereis locates binaries, source files, and manual pages, limits each search type, lists effective paths, and differs from which and find.

Linux Directory Structure Explained: Purpose of Major Directories

Linux Directory Structure Explained: Purpose of Major Directories

Understand the purpose of major Linux directories such as /etc, /var, /usr, /home, /proc, and /sys, and learn where common system files belong.