Using bzip2 Command In Linux- Compress/Decompress Files

bzip2 command in Linux is probably a way allowing users to compress and decompress files. Means, with the help of this command, users can bind the files into a single file which occupies less disk space in compare to those files are saved originally. Technically, this command has a slower decompression time and takes higher memory in use. When this runs over files, each file is replaced with a compressed version of itself, while the name of files remain same with an extension .bz2.

How to use bzip2 command in Linux?

Before you use bzip2 command, it’s necessary to install it by following commands below according to Linux distribution:

For Debian/Ubuntu

sudo apt install bzip2

For CentOS/RHEL

sudo yum install bzip2

For Fedora

sudo dnf install bzip2

Syntax of bzip2 command

bzip2 [Options] filenames…

Options available to use with bzip2 command

  • -z: This option is used to enforce compression.
  • -k: Used for compression of files along with keeping original files.
  • -d: Used for decompression of compressed files.
  • -t: Used for performing an integrity check of the file to determine if the file is corrupt or not.
  • -v: Displays compression ratio of each processed file.
  • -h | -help: displays help information and exits.
  • -L | -license: Displays license terms and conditions
  • -V | -version: Displays version details and exits.
  • -q | -quiet: Used for suppressing non-essential warnings, but not the I/O errors and other critical events.
  • -f | -force: Used for enforcing overwrite of output files.

How to compress files using bzip2 command in Linux

To compress a file using bzip2 command, users can run the following syntax:

bzip2 file_name
or
bzip2 -z file_name

To compress a file and keep the original files as well:

bzip2 -zk file_name

How to decompress files using bzip2 command?

To decompress a compressed file:

bzip2 -d file_name.bz2