Command line snippet of the week: 48

Hate having to remember all switches for each different unpacker? You can fix that!
Create a new file in /usr/bin/ called “extract” and paste the following code:

if [ -f $1 ] ; then
case $1 in
    *.tar.bz2)   tar xvjf $1        ;;
    *.tar.gz)    tar xvzf $1     ;;
    *.bz2)       bunzip2 $1       ;;
    *.rar)       unrar x $1     ;;
    *.gz)        gunzip $1     ;;
    *.tar)       tar xvf $1        ;;
    *.tbz2)      tar xvjf $1      ;;
    *.tgz)       tar xvzf $1       ;;
    *.zip)       unzip $1     ;;
    *.Z)         uncompress $1  ;;
    *.7z)        7z x $1    ;;
    *)           echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file"
fi

be sure to chmod it to executable:

chmod +x /usr/bin/extract

This code will save you time by extracting almost any kind of archive by using the command:

extract my_awesome_archive.tar.gz
 

What other people think

  1. Christian - 26th of November, 2009

    Thanx! Great script :-)

    Probably gonna save me a lot of time in the future.

  2. Thierry - 27th of November, 2009

    very usefull, thanks very much !!

  3. FLX - 27th of November, 2009

    Glad to hear you guys like it. I’ll be posting interesting command line snippets every thursday, be sure to check them out!