Register
It is currently Mon Nov 24, 2014 7:49 pm

WOP's First Script "dex"


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Sun May 13, 2007 4:15 pm   

Joined: Sun May 13, 2007 4:05 pm
Posts: 2
I wanted to make this script as I would have assumed something like this allready exists but as far as I know, when you download a tar source zipped file, you have to tar -xvvzf <thefile> or whatever every time. That gets old after a while and should be automated. The name of the script is "dex" for Decompress and EXtract.
syntax: $dex <whatever>. I'm aware its very simple but I find its very useful. I hope to get into much more advanced stuff later which is why I registered here and hope to be a part of the community.
Thanks

#!/bin/bash
# This script decompresses the file you specify and then extracts it. Used for Files like..
# .tar.gz, .tar.bz2

# If the file is a Gzipped file
if [ $1 = *.tar.gz ]; then
tar -xvzf $1
echo "Decompressed and Extracted $1"
fi


# If the file is a Bzipped file
if [ $1 = *.tar.bz2 ]; then
tar -xvjf $1
echo "Decompressed and Extracted $1"
fi


Top
 Profile WWW  
 PostPosted: Mon May 14, 2007 12:26 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Nice script :) Glad to have you here :)

Here is a script that is similar, that decompresses several types of formats also:
Code:
#!/bin/bash
# use extract function

extract () {
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   tar xjf $1        ;;
             *.tar.gz)    tar xzf $1     ;;
             *.bz2)       bunzip2 $1       ;;
             *.rar)       rar x $1     ;;
             *.gz)        gunzip $1     ;;
             *.tar)       tar xf $1        ;;
             *.tbz2)      tar xjf $1      ;;
             *.tgz)       tar xzf $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
}

#call extract function
extract $1
exit


i have that in my .bashrc file as just the function, so i can simply type "extract somenamehere.tar.gz" ..... makes it fast and easy to remember :)


Top
 Profile WWW  
 PostPosted: Mon May 14, 2007 1:44 pm   

Joined: Tue May 08, 2007 3:49 pm
Posts: 5
hi, I have the same script in my .bashrc but a little bit improved.
cause the original has a bug. it can not extract files with spaces in the filename.
the solution is to put the variable in quotes eg. "$1"

Code:
extract () {
   if [ -f "$1" ] ; then
     case "$1" in
    *.tar.bz2)   tar xjf "$1"        ;;
    *.tar.gz)    tar xzf "$1"     ;;
    *.bz2)       bunzip2 "$1"       ;;
    *.rar)       rar x "$1"     ;;
    *.gz)        gunzip "$1"     ;;
    *.tar)       tar xf "$1"        ;;
    *.tbz2)      tar xjf "$1"      ;;
    *.tgz)       tar xzf "$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
}


Top
 Profile  
 PostPosted: Mon Jul 30, 2007 6:46 am   

Joined: Wed Jul 25, 2007 11:45 am
Posts: 11
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

}



Top
 Profile  
 PostPosted: Tue Jul 31, 2007 1:14 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Nice..... thanks :)


Top
 Profile WWW  
 PostPosted: Tue Mar 11, 2008 7:10 am   

Joined: Sun Mar 09, 2008 7:35 pm
Posts: 4
Modified this script a tiny little bit to download the file first.
Requires `wget` and `basename`:

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

}

FILE=`basename $1`
wget $1

#call extract function
extract $FILE
exit


Top
 Profile  
 PostPosted: Thu Aug 28, 2008 6:35 am   

Joined: Thu Aug 28, 2008 4:15 am
Posts: 3
Location: India
i'm a lil new @ this, but whats basename supposed to be?


Top
 Profile YIM  
 PostPosted: Wed Jun 24, 2009 5:05 pm   

Joined: Sun Mar 09, 2008 7:35 pm
Posts: 4
# basename http://www.someurl.invalid/somefile
somefile

B-)


Top
 Profile  
 PostPosted: Thu Jun 25, 2009 1:41 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
basename shortens a path.

http://linux.die.net/man/3/basename

This explains it more in detail :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP