agmblq
Last Updated: February 25, 2016
·
4.399K
· dlion
28864ba1e7e4490c3e5e97e095d4b6c7

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.

Say Thanks
Respond

6 Responses
Add your response

6445
Kotozayc

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

over 1 year ago ·
6452
28864ba1e7e4490c3e5e97e095d4b6c7

@xvoland Thank you to share it!

over 1 year ago ·
16835
A6088cf5564c0e0028c7982b2ac43ca1

@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
A6088cf5564c0e0028c7982b2ac43ca1

@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
Kotozayc

@terminalroot

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

over 1 year ago ·
18260
1d6eeb33a8d967f618301516bba16521

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

over 1 year ago ·
Filed Under