Bzip2 Command in Linux with Examples

Today you will learn how to compress files in Linux using bzip2 command.

What is File Compression?

File compression is a method by which we can save disk space by reducing the size of available data. A compressed file is a collection in which one or more files are stored.

We have several ways in Linux by which you can compress files and directories. The most popular tools are:

  • gzip
  • bzip2
  • zip

Suggested Read: Gzip Command in Linux with Examples

In this guide, we are going to discuss the complete features of bzip2 command with examples.

bzip2 is the most popular compression tool among other similar tools used to compress files only.

But by combining it with the tar command, you can also compress directories.

Features of bzip2 Command:

  • Compress and Decompress (Extract) a File
  • Reduces file size
  • Display the contents of a compressed file
  • Tests compressed file integrity
  • Concatenate the contents of multiple files.

How to identify a bzip2 file?

You can identify the bzip2 file by its extension. The file extension of bzip2 is .bz2.

First of all, let’s focus on some of the most important options that we can use with the bzip2 command.

OptionsExplanation
-h, --helpDisplay the Help page of bzip2 Command
-d, --decompressForce decompression
-z --compressForce compression
-k, --keepKeep (don't delete) input files
-f, --forceOverwrite existing output files
-t, --testTest compressed file integrity
-c, --stdoutCompress or decompress to standard output.
-q, --quietSuppress noncritical error messages
-v, --verboseVerbose mode
-L, --licenseDisplay software version & license
-V, --versionDisplay software version & license
-s --smallUse less memory (at most 2500k)
-1, --fastCompress Faster
-9, --bestCompress Better

You have to follow the below syntax to use bzip2 command.

bzip2 [OPTION]... [FILE]...

1. Compress a File

To compress a file using bzip2 type the following command.

Here I am compressing a file named file.txt.

~$ bzip2 file.txt

Output:

~$ ls
file.txt.bz2
Compress a File

2. Compress multiple Files

You can compress multiple files using bzip2.

To compress multiple files, pass the file names to bzip2. Here I am compressing files named file.txt, file1.txt, file2.txt, file3.txt, file4.txt

~$ bzip2 file.txt file1.txt file2.txt file3.txt file4.txt

Output:

~$ ls
file1.txt.bz2  file2.txt.bz2  file3.txt.bz2  file4.txt.bz2  file.txt.bz2
Compress Multiple Files

You can also use wildcards with bzip2. Let’s take some examples:

Ex. # 1 Compress those files whose extension is “.txt“.

~$ bzip2 *.txt

Ex. # 2 Compress those files that start with “fi“.

~$ bzip2 fi*

3. Compress a file Forcefully

To compress a file forcefully pass the -z option to bzip2.

~$ bzip2 -z file.txt

4. Keep (Don’t delete) input Files

bzip2 by default deletes the input file after compressing.

But if you want to keep the input files during compression, then pass the -k option to bzip2.

~$ bzip2 -k file.txt

Output:

~$ ls
file.txt  file.txt.bz2

By the way, the -c option helps to concatenate the contents of multiple files.

But you can also use this option to keep input files while compressing.

Here is an example.

Syntax:

bzip2 -c [INPUT FILE] > [OUTPUT FILE]
~$ bzip2 -c file.txt > newfile.txt.bz2

Now type the following command to concatenate the contents of the two files.

Here in this example, I am concatenating the contents of file.txt and file1.txt.

~$ bzip2 -c file1.txt >> newfile.txt.bz2

Now to decompress the file type the following command.

~$ bzip2 -d newfile.txt.bz2

Output:

~$ ls
newfile.txt

~$ cat newfile.txt 
Linux is a Open Source Operating System.
I Love Linux.

5. Test compressed file integrity

To test the compressed file integrity pass -t option to bzip2 Command.

You must be wondering what is this integrity.

Integrity means that we can check whether a Particular .bz2 file is valid or not.

Now you might be wondering what is the need to check validity.

Let me show you an example.

What if I create a .bz2 file using mv command. Refer to the following command.

~$ mv file.txt file.txt.bz2

Now let’s check the integrity of this file.

~$ bzip2 -t file.txt.bz2 
bzip2: file.txt.bz2: bad magic number (file not created by bzip2)

You can use the `bzip2recover' program to attempt to recover
data from undamaged sections of corrupted files.

As you can see on the output above we received an error that “bad magic number (file not created by bzip2)” because we have not created this file using bzip2 command.

A valid .bz2 file will display the following output.

~$ bzip2 -tv file.txt.bz2 
  file.txt.bz2: ok

6. Decompress (Extract) a File

To Decompress (Extract) a .bz2 file pass the -d option to bzip2.

~$ bzip2 -d file.txt.bz2

OR you can use the bunzip2 command to extract a .bz2 file.

~$ bunzip2 file.txt.bz2

7. Decompress multiple files

To decompress multiple files, pass the file names to bzip2. Here I am decompressing files named file1.txt, file2.txt, file3.txt, file4.txt, file.txt.

~$ bzip2 -d file1.txt.bz2  file2.txt.bz2  file3.txt.bz2  file4.txt.bz2  file.txt.bz2

You can also use wildcards with bzip2. Let’s take some examples:

Ex. # 1 Decompress those files whose extension is “.bz2

~$ bzip2 -d *.bz2

Ex. # 2 Decompress those files that start with “fi

~$ bzip2 -d fi*

8. Overwrite existing output files Forcefully

bzip2 command allows us to Overwrite existing output files forcefully.

To do so pass the -f option to bzip2 command.

Let’s take an example.

Overwrite existing output file while Compressing:

I have a file named file.txt and Compressed version of this file is file.txt.bz2

~$ ls
file.txt  file.txt.bz2

For some reason again I want to compress the same file using bzip2. So Let’s do it.

~$ bzip2 file.txt 
bzip2: Output file file.txt.bz2 already exists.

As you can see on the output above, Command is not allowing us to compress the file saying “Output file file.txt.bz2 already exists”

In such a scenario, you can use -f option to complete the task forcefully.

Refer to the following command.

~$ bzip2 -f file.txt

Overwrite existing output file while decompressing:

Similarly, Overwrite existing output file while decompressing using -f option.

~$ bzip2 -df file.txt.bz2

9. bzip2 Command Compression Level’s

bzip2 allows to compress files using several compression levels which is ranges from 1 to 9.

If you do not specify a level with the bzip2 command then it uses compression level -6 to compress files.

Compress Faster:

You can use -1 for the fastest compression speed with a lesser compression ratio.

~$ bzip2 -1 file.txt

Compress Better:

You can use -9 for lowest compression speed with maximum compression ratio.

~$ bzip2 -9 file.txt

10. Display output of a Command

To display the verbose output of the command pass the -v option to bzip2.

Example # 1

~$ bzip2 -v file.txt 
  file.txt:  0.526:1, 15.220 bits/byte, -90.24% saved, 41 in, 78 out.

Example # 2

~/data$ bzip2 -dv file.txt.bz2 
  file.txt.bz2: done

11. Display the contents of a Compressed File

You can display the contents of a compressed file using the bzcat command.

Let’s take an example:

I have a file named file.txt with some content. To display the contents of this file you can use the cat command.

~$ cat file.txt 
Linux is a Open Source Operating System.

Let’s compress the file using bzip2 command.

~$ bzip2 file.txt

Now to display the contents of file.txt.bz2 using bzcat type the following command.

~$ bzcat file.txt.bz2 
Linux is a Open Source Operating System.

Type the following command to display the contents of multiple compressed files.

In this example, i am displaying the content of files named file1.txt.bz2 and file.txt.bz2

~$ bzcat file1.txt.bz2 file.txt.bz2 
I Love Linux
Linux is a Open Source Operating System.

12. Reduce memory Usage

To reduce memory usage, for compression, decompression pass -s option to bzip2 command.

In short, if your system is low on memory you should use this option.

~$ bzip2 -s file.txt

13. Display the Help page of gzip Command

Use the following commands to access the Manual Page/Help Page of bzip2 command.

~$ bzip2 --help
~$ man bzip2

14. Display Software License

To display Software License information of bzip2 command use -L option.

~$ bzip2 -L

15. Display Version

Check the bzip2 command version using the following command.

~$ bzip2 -V

You can visit at following websites to get more information on bzip2.

Infographic

Refer to this Infographic for complete bzip2 command options.

Bzip2 Command

Conclusion

I hope you have learned something from this article.

I have tried my best to include all the features of bzip2 command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment