Session and utilities
The bzip2 command
Compresses to .bz2: usually smaller than gzip, at the cost of more time.
Most useful options
-cWrites the result to standard output and leaves the originals untouched-dDecompresses. Same as bunzip2-fOverwrites the output file if it already exists-kKeeps the original. Without this option, bzip2 replaces it with the .bz2-tChecks the file's integrity without writing the decompressed version-vShows the compression ratio of each file-1 a -9Block size when compressing. -9 is the default and compresses the most; it has no effect when decompressingCopy-ready examples
$ printf 'texto\n' > f.txt && bzip2 -k f.txt && lscompresses a file and keeps the original.Example output
f.txt f.txt.bz2
$ bzip2 -c informe.txt > informe.txt.bz2creates the .bz2 with a redirect, without touching the original.$ bzip2 -t f.txt.bz2checks that the .bz2 is not damaged, without decompressing it to disk.$ tar -cf - carpeta | bzip2 > carpeta.tar.bz2packs a folder and compresses it in one step. With no file names, bzip2 reads from standard input.$ bzip2 -f informe.txtreplaces informe.txt with informe.txt.bz2 and overwrites an earlier .bz2 if one exists. Warning: you lose that earlier .bz2.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| bzip2: Output file f.txt.bz2 already exists. | A .bz2 with that name already exists and bzip2 does not overwrite it by default. | Rename or delete the earlier .bz2, or use -f if you want to replace it. |
| bzip2: Can't open input file nope.bz2: No such file or directory. | The file you named does not exist or the name is misspelled. | List the folder with ls and copy the exact name. |
Typical mistakes
- Without -k, bzip2 deletes the original when it finishes. If you want to keep it, add -k.
- bzip2 does not overwrite a .bz2 that already exists. Use -f only when you know the earlier one is not needed.
- Files under about one hundred bytes usually come out larger: the header weighs more than the content.
Differences from similar commands
- gzip: uses the LZ77 method and has its own .gz format. bzip2 sorts blocks of data: it usually compresses more, but takes longer.
- xz: another compressor with its own format (.xz). It usually compresses more than bzip2, and it is more common in software packages today.
- bzcat: decompresses a .bz2 to standard output. bzip2 compresses, bzcat only reads.
FAQ
When should I choose bzip2 over gzip?
When size matters more than time. For general exchange, gzip is still the most widespread format.
Can I compress several folders at once?
Each file becomes its own .bz2. For a set of files, pack them first with tar and compress the result.
What does -9 mean?
The block size: 900 k by default. Larger blocks compress slightly better at the cost of more memory when compressing. It has no effect when decompressing.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course