Archives and compression

Command bzip2

Compress file using bzip2 algorithm

Linux · Bash examples · commands run on the selected server

When to use bzip2

Use bzip2 to compress a single file or validate a bzip2 stream. Compression is not archiving: use an archive format when you need a directory tree, names and metadata packaged together.

Syntax

bzip2 [OPTIONS] FILE…

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-k
Save the original file.
-d
Unpack .bz2.
-t
Check the integrity of the compressed stream.

Usage examples

Compress while preserving the source

bzip2 -k ./report.txt

A compressed copy is created while retaining the original text file.

Check archived stream

bzip2 -t ./report.txt.bz2

The compressed stream is checked for internal integrity without extracting a new file.

Unpack into a separate file

bzip2 -dc ./report.txt.bz2 > ./report-restored.txt

The decompressed content replaces a separate restored-file path.

Notes and limitations

  • bzip2 compresses a stream, not a directory tree. Use tar to collect directories into an archive.
  • Without -k, a successful operation normally removes the source file. Extract into a suitable directory without name conflicts.

Common questions and troubleshooting

Does a successful integrity test prove this is the right backup?

No. It checks the compressed stream, not whether the original data was correct or complete. Verify the expected contents and restore procedure.

Why did compression remove my source file before?

Without keep mode, common bzip2 behavior replaces the input with its compressed form. Choose the intended retention behavior before running it.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man bzip2.