FactorPad
Build a Better Process

Linux bzip2 Command Summary with Examples

Linux bzip2 allows you to compress and decompress files.
  1. Purpose - Learn what bzip2 is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with bzip2.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux bzip2, 70 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / bzip2-command.html


An ad-free and cookie-free website.


The Linux bzip2 command

Beginner

Learn to compress and decompress files at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux bzip2 Command Summary with Examples (4:13)

Video Script

The Command and Why You Need It

Our seventieth word, or command to memorize is bzip2 from our category Files.

bzip2 allows you to compress and decompress files.

Common Linux bzip2 Options
-options description
-h, --help Print help screen
-k, --keep Keep input file
-v, --verbose Report percent reduction in file size

Recall from the two videos on gzip and gunzip we saw a first compression algorithm. Now we'll evaluate another, promising better results.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The bzip2 command has 14 options and the argument is the file or list to compress, and note, filename extensions here are .bz or more commonly .bz2.

Like most commands, help is available with double-dash --help. The -k option keeps the input file and -v shows file compression details.

So why is bzip2 an important command? Well, at times you're given compressed files with .bz or .bz2 extensions and you need to know how to handle them. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. As in the last video, we created a file from the man command on gunzip and wrote it to a file called video69a.txt, copied it to video69b.txt and then gzipped it.

$ ls -og video69* -rw-r--r-- 1 17752 Nov 5 19:15 video69a.txt -rw-r--r-- 1 17752 Nov 5 19:15 video69b.txt -rw-r--r-- 1 6366 Nov 5 19:15 video69b.txt.gz

So let's see the compression stats on the file using gunzip -l video69b.txt.gz.

$ gunzip -l video69b.txt.gz compressed uncompressed ratio uncompressed_name 6366 17752 64.3% video69b.txt

And then we notice that it compressed it at 64%, so that's our bogey and what we're trying to beat.

Next, let's use that same starting file, so video69b.txt, and copy it to video70.txt. And then zip it using bzip2 -v to see the contents and compression stats.

$ cp video69b.txt video70.txt $ bzip2 -v video70.txt video70.txt: 3.030:1, 2.640 bits/byte, 67.00% saved, 17752 in, 5859 out.

See, we have it at 67%, so a little bit better.

And then an ls -og video70* just to see what's out there.

$ ls -og video70* -rw-r--r-- 1 5859 Nov 6 10:15 video70.txt.bz2

And notice we only have that new file (meaning it replaces the input file with the new).

And last, let's decompress using bunzip2 -k to keep the file, right? On video70.txt.bz2 that we just created. And an ls -og and we should see two files.

$ bunzip2 -k video70.txt.bz2 $ ls -og video70* -rw-r--r-- 1 17752 Nov 6 10:15 video70.txt -rw-r--r-- 1 5859 Nov 6 10:15 video70.txt.bz2

A Final Tip

Okay now you know how to use bzip2. And you know the syntax for commands, options and arguments.

One last tip about the bzip2 command. So just as with the gzip/gunzip combo, the bzip2 manual page is shared between the compression and decompression commands, so check that out.

Okay, thanks for visiting today. I hope this was a helpful introduction to the bzip2 command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Our group is democratizing knowledge and data so everyone has the power to make better financial decisions. Subscribe, follow and contribute to be a part of it.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / bzip2-command.html


bzip2
linux bzip2 examples
bzip2 linux
bzip2 unzip
unzip bz2 file
extract bz2
uncompress bz2 file
bzip2 decompress
bz2 decompression
open bz2 file
open bz file
bz file
extract bz2
bzip2 example
bz2 extension
bzip2 command tutorial
bzip2 command examples
bz2 extension
linux file compression
linux compress files
linux decompress files
linux decompress files
linux decompress directories

A newly-updated free resource. Connect and refer a friend today.