Compress file or directory using the BZIP2 command in Linux

Home > Search > Linux commands
  by

The bzip2 command can be used to compress files or directories. Let's take an example where /home/john.doe contains a few files and directories, where example.file is 672 bytes.

[john.doe@server1 ~]# ls -l /home/john.doe
-rw-r--r-- john.doe john.doe 672 Jan 01 :17:16 example.file

 

The bzip2 command can be used to compress example.file.

[john.doe@server1 ~]# bzip2 example.file

 

This will create a new file named example.file.bz2. In this example, the compressed file is 69 bytes.

[john.doe@server1 ~]# ls -l /home/john.doe
-rw-r--r-- john.doe john.doe 69 Jan 01 :17:16 example.file.bz2

 


Keep the compressed file

By default, bzip2 will remove the original file, creating a new compressed file. The -k or --keep option can be used to save the original file, and also create a new compressed file. 

[john.doe@server1 ~]# bzip2 -k /home/john.doe/example.file

 

In this example, the original example.file is kept, along with the creation of a bzip2 compressed file.

[john.doe@server1 ~]# ls -l /home/john.doe
-rw-r--r-- john.doe john.doe 672 Jan 01 :17:16 example.file
-rw-r--r-- john.doe john.doe  69 Jan 01 :17:16 example.file.bz2

 


Decompress

The -d or --decompress option can be used to uncompress a bzip2 compressed file. In this example, example.file.bz2 is uncompressed. Similarly, the bunzip2 command can also be used to decompress a bzip2 compressed file.

[john.doe@server1 ~]# bzip2 -d example.file.gz

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments