FactorPad
Faster Learning Tutorials

Linux bzip2 command summary with examples

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.
Paul Alan Davis, CFA, November 6, 2016
Updated: August 5, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / bzip2 command


The Linux bzip2 command

Beginner

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

Video Tutorial

Linux bzip2 command summary with examples (4:13)

Videos can also be accessed from the YouTube Playlist.

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.

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?

As always, reach us on social media, including our YouTube Channel. Here's the easiest way to subscribe.

  • For the Outline of all 100 tutorials, click Outline.
  • To go back to the gunzip command, hit Back.
  • To learn a few more compression algorithms, including xz, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / bzip2 command



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