Bootstrap FreeKB - Linux Commands - bzip2 (compress file)
Linux Commands - bzip2 (compress file)

Updated:   |  Linux Commands articles

The bzip2 command can be used to compress files. The tar command an be used to compress a directory.

In this example, the /home/john.doe directory contains a single file, example.txt. The example.txt file is not compressed, and is 672 bytes.

ls -l /home/john.doe

-rw-r--r-- john.doe john.doe 672 Jan 01 :17:16 example.txt

 

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

bzip2 example.txt

 

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

ls -l /home/john.doe

-rw-r--r-- john.doe john.doe 69 Jan 01 :17:16 example.txt.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. 

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.

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.

bzip2 -d example.file.gz

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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