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 locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux csplit Command: Split Files at Lines or Patterns

Linux csplit Command: Split Files at Lines or Patterns

Learn how to split Files at Lines or Patterns with the Linux csplit command, including practical examples, key options, and important precautions.

Linux readlink Command: Inspect Symbolic Link Targets

Linux readlink Command: Inspect Symbolic Link Targets

Learn how to use the Linux readlink command to display symbolic link targets and optionally resolve canonical paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux pkill Command: Signal Processes by Name

Linux pkill Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux pkill command, including practical examples, key options, and important precautions.

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.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.