agmblq
Last Updated: February 25, 2016
·
4.839K
· dlion

Bash function for extract every archive with one word

function extract()
{
     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
}

For use the function you can write on the shell:
extract example.tar.gz

Really usefull and fast for me.

6 Responses
Add your response

6445

I think this https://github.com/xvoland/Extract version is better

over 1 year ago ·
6452

@xvoland Thank you to share it!

over 1 year ago ·
16835

@xvoland in the link show for you, change:
this:
[ -z "$1" ]; then
for this:
[ -n "$1" ]; then

and
remove the else

www.terminalroot.com.br

over 1 year ago ·
16836

@xvoland in the link show for you, change: this: [ -z "$1" ]; then for this: [ -n "$1" ]; then

and remove the else

www.terminalroot.com.br

over 1 year ago ·
16837

@terminalroot

Too bad idea :) The script will not work correctly, if I do

over 1 year ago ·
18260

This page inspired me to make this: https://www.npmjs.com/package/extract-cli

over 1 year ago ·
Filed Under