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:

#!/bin/bash
# trex - tar files and folders

NAME=$1
LOCATION="$*"

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

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

References