Commands.page Logo

Compress Single File with bzip2 Command in Ubuntu

This article provides a concise guide on using the bzip2 compression tool within the Ubuntu operating system. It details the specific command syntax required to compress individual files, explains how to retain the original file during the process, and outlines how to decompress the data when needed.

The Basic Compression Command

To compress a single file using bzip2, open your terminal and navigate to the directory containing the file. The primary command is simply bzip2 followed by the filename.

bzip2 filename.txt

Running this command will replace the original filename.txt with a compressed version named filename.txt.bz2. The original file is deleted by default to save space.

Keeping the Original File

If you wish to compress the file but keep the original copy intact, use the -k (keep) option.

bzip2 -k filename.txt

This creates the filename.txt.bz2 compressed file while leaving filename.txt untouched in the same directory.

Decompressing the File

To restore the file to its original state, use the -d (decompress) flag followed by the compressed filename.

bzip2 -d filename.txt.bz2

Alternatively, you can use the bunzip2 command, which performs the same function.

bunzip2 filename.txt.bz2

These commands will remove the .bz2 extension and restore the original file content.