The Three Amigos – Tar, bz2, and gunzip

Lucky Day: Wherever there is injustice, you will find us.
Ned Nederlander: Wherever there is suffering, we’ll be there.
Dusty Bottoms: Wherever liberty is threatened, you will find…
Lucky Day, Ned Nederlander, Dusty Bottoms: The Three Amigos!

Dusty Bottoms: Do you have anything here besides Mexican food?

Tar, bz2, and gunzipBeginning with Linux meant getting to know the obscure file.tar.gz I had sometimes seen trespassing in Windows. To begin with, this extra dot threw me off – what does that mean? And what about these extensions that weren’t always the same, similar but different – .tgz, tar.bz2, tb2. Well here is what tar is and his amigos gunzip and bunzip2.

Tar is a file format and a program. Tar is short for tape archive, whence once tape media was used to backup data. Tar is a program to create a file that is a container of other files. Tar follows the Unix Philosophy “do one thing and do it well” paradigm and is ONLY a container of files. Tar files are not compressed (though they are typically compressed afterword to save space which are typically termed tarballs). Most tar compressions are either done with the gzip or the bzip2 programs. Why not compress a bunch of files with just gzip or bzip2 then? Because gzip and bzip2 follow the Unix Philosophy as well and can only compress one file. The tar program only has the ability to concatenate files, gzip and bzip2 have the ability to compress single files.Tar, bz2, and gunzip

Tar though does have built-in ability to work in conjunction with gzip and bzip2 compression (bzip2 is becoming a more popular format because it provides provides higher compression).

To tar and compress with gzip:

tar -cvpzf filename.tar.gz fileorfolder

To tar and compress with bzip2:

tar -cvpjf filename.tar.bz2 fileorfolder
  • -c is for create, v verbose, p preserve permissions, z gunzip, j is bunzip2, f is write to file
  • these arguments cannot generally be switched, the order is important.
  • tar.gz is the typical Unix format. For MSDOS compatibility sometimes .tgz or .bz2 will be seen.
  • any number of files can be specified file1 file3…

To see the list of files in a tar container:

tar -tzf filename.tar.gz
tar -tjf filename.tar.bz2

And to decompress:

tar -xvf file.tar.tgz
tar -xvf file.tar.bz2

I have experience with the new simplified way (-xvf) not always working and had to use the expanded form for bunzip2’s:

tar xvjpf file.bz2

Because I’m not good at remembering arguments I created a basic script called trex:

# trex - tar files and folders


if [[ -z $LOCATION ]]; then
    echo "tarer <compressed-name>.tgz </location/to/start>"
    echo "  - use absolute location if planning restores."

tar -czpvf "$NAME".tgz "$LOCATION"



8 thoughts on “The Three Amigos – Tar, bz2, and gunzip

  1. Ulrich

    This is a very good post. Very descriptive and similar to the problems I had when I started out with Linux. Didn’t have a clue what all these tar’s are for :P .

    I also like the script – if you don’t mind I’d like to add it to my site with your permission since I also have a similar article at my site (how to install software in linux).

  2. Jinks

    A small correction:

    the compression programs are called gzip and bzip2, gunzip and bunzip2 are usually just symbolic links equivalent to calling gzip -d (decompress) or bzip2 -d.

  3. Dirk Gently Post author

    Thanks alot Ulrich. Please be free to use the script however think is best.

    Ah, lol, gunzip bunzip2 of course. I’ll fix it in the morning.

  4. Dariush

    After a lot searching in Linux-Unix jargon to explain these three amigos I found them clearly explained here. Thank you


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s