Week of bash scripts – Unpack

As the antithesis of yesterdays pack command there is unpack.


This script was originally written by brisbin33 in the Arch forums (beautiful work man) and is slightly-modified. It will unpack most compressed file formats. Syntax is:

unpack <archive.ext>

About Todd Partridge (Gently)

Good times, good people, good fun.

Posted on 2009-08-04, in Linux, Script. Bookmark the permalink. 6 Comments.

  1. tar is able to detect what kind of compression is used, so “tar xvf $1” is sufficient. So this should be enough:

    case $1 in

    *.tar|*.tar.gz|*.tar.bz2|*.tar.lzma|*.tar.xz|*.tgz|*.tbz2) tar xvf $1 ${2:+-C $2} ;;

  2. Yeah, I know that tar does this now but decided to leave this in because sometimes I work in the BSD world and tar is a bit more legacy there. However that command is very, Nice! Didn’t know that tar could do lzma. Could you explain what the ${2:+-C $2} does?

  3. Doesn’t BSD use the same GNU-packages as Linux? Then it should just work… give it a try and you know :)
    For gzip and bzip2 at last it should, those are included for ages.
    Support for lzma was added about a year ago, xz a few months ago.

    ${var:+something} means: if $var is set and not empty, then use “something”.
    It’s the reverse of ${var:-something}, which means: if $var is empty or does not exist, then use “something”. Very handy.

    If you leave out the colon, the empty-part doesn’t apply.
    Example: ${var-text} means: if $var doesn’t exist, then use “text”. NOT if $var does exist, but is unset/null/blank.

    So ${2:+-C $2} means: if you specified a directory ($2), then use “tar xvf -C $2”, otherwise use “tar xvf”. A little trick to modify parameter $2 :)

    A few more of these: http://www.delorie.com/gnu/docs/bash/bashref_29.html

  4. Yeah, BSD utils can be different. I usually use OpenBSD and tools there are not the same. I think it has to do with the BSD and GPL licenses, but I’ve tried NetBSD… Some options will be there, not others, and some even a re completely different.

    Always good to get these bash tips. Nice.

  1. Pingback: Script d’extraction universel | Tito's Geeking Logs

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: