Making a single extractor

One of my new articles is on smplifying your command line (read more about System Administrators Toolkit: Standardizing your UNIX command-line tools, making your life easier as you move between different environments. The same principles can be applied just to make your life easier. Here’s a function I’ve had in my bash init script for years that gets round the issue of extracting a compressed archive file of various types, even if your tar isn’t aware of the compression type:

function uz ()
{
    file=$1
    case $file in
        (*gz)  gunzip -c $file|tar xf -;;
        (*bz2) bunzip2 -c $file|tar xf -;;
        (*Z) tar zxf $file;;
        (*zip) unzip $file;;
    esac
}

Now I can extract any file with:

$ uz file{gz|bz2|zip|Z)

And not worry that my Solaris tar isn’t bzip2 aware even though it is Gzip aware.