Modified the script a bit, because on Solaris GNU tar is not always installed. Which means using `gzip -dc | tar xvf -' is required, since tar zxvf is not supported. This keeps the original compressed file, so we delete this as well.
Improved the case for .tgz and .tbz2 files as well, so we have only one action for both type of naming conventions. And added the -r test to check wheter the file is readable. With tar I like verbosity, so added this as well.
Code:
#!/bin/bash
# use extract function
extract () {
# If not file or not readable, abort actions
[ ! -f "$1" ] || [ ! -r "$1" ] && echo "'$1' is not present or could not be read" && return
# Do magic
case $1 in
*.tar.bz2|*.tbz2) bzip2 -dc $1 | tar xvf - ; rm $1 ;;
*.tar.gz|*.tgz) gzip -dc $1 | tar xvf - ; rm $1 ;;
*.bz2) bzip2 -d $1 ;;
*.rar) rar x $1 ;;
*.gz) gzip -d $1 ;;
*.tar) tar xvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
}