Register
It is currently Thu Oct 23, 2014 8:26 am

.bashrc -- extract compressed files easier.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Aug 21, 2006 11:47 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
I found this one on the arch linux forums --- kind of a cool use of the .bashrc file ;)

Code:
ex () {
    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 WWW  
 PostPosted: Tue Aug 22, 2006 2:34 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
ya... arch forums have a ton of goodies hidden within itself :)
never saw that one... nice find :)
i'm assuming now... to 'ex'tract something... you just use:
ex <filename>
and it extracts it by the extension... very cool :wink:


Top
 Profile  
 PostPosted: Tue Aug 22, 2006 6:18 am   
User avatar

Joined: Tue May 17, 2005 12:55 am
Posts: 86
Location: Big Easy, Louisiana
i'll be the newb, and ask what that could be used for. I understand vaguely that it will extract files from archives. correct.


Top
 Profile YIM  
 PostPosted: Tue Aug 22, 2006 11:11 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Yep, It's basically a function that runs different commands based on the file extension.

"ex" I assume is short for "extract". So if you run "ex myarchive.tar.bz2", the script takes the file name and matches it in the case statement, and runs the associated command.

This way you can just run:

ex myarchive.tar.bz2

instead of typing out:

tar jxvf myarchive.tar.bz2


It saves typing, and saves you from having to look up the program and switches that you need every time you need to extract an archive type that you don't already have committed to muscle memory :)

I hope that didn't make it worse...
-Jeo


Top
 Profile YIM  
 PostPosted: Sat Sep 02, 2006 11:13 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Code:
compress () {
   FILE=$1
   case $FILE in
      *.tar.bz2) shift && tar cjf $FILE $* ;;
      *.tar.gz) shift && tar czf $FILE $* ;;
      *.tgz) shift && tar czf $FILE $* ;;
      *.zip) shift && zip $FILE $* ;;
      *.rar) shift && rar $FILE $* ;;
   esac


Top
 Profile WWW  
 PostPosted: Tue Apr 01, 2008 11:19 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
A bit cleaner:
Code:
compress () {
   FILE=$1
   shift
   case $FILE in
      *.tar.bz2) tar cjf $FILE $*  ;;
      *.tar.gz)  tar czf $FILE $*  ;;
      *.tgz)     tar czf $FILE $*  ;;
      *.zip)     zip $FILE $*      ;;
      *.rar)     rar $FILE $*      ;;
      *)         echo "Filetype not recognized" ;;
   esac
}


Also, for extract(), what about using "file" rather than matching the extension. Would it be better, or would it be overkill?


Top
 Profile  
 PostPosted: Fri Mar 12, 2010 2:40 pm   

Joined: Wed Mar 10, 2010 8:05 pm
Posts: 25
I liked the idea of your extragng function. I took it and built it into a more featureful app. hope fully you don't mind that I GPL'd it since it's quite large:

Code:
#!/bin/bash

e=""

function printhelp {
  echo "Usage: x [files] [option]"
  echo " extracts an archive file. options not shown below are passed to the extracting app."
  echo " use {} anywhere in those passed options and the file name (without the extension) of"
  echo " the archive will be placed there."
  echo ""
  echo "Options:"
  echo "        -xp  --xpretend    print what WOULD be executed, but don't actually execute."
  echo "        -xf  --xfork       execute all commands in parellel instead of one-by-one"
  echo "        -xh  --xhelp       print help"
  echo "        -xv  --xversion    print version"
}

function printversion {
  echo "x - extract various types of archives files."
}

if [ -z "$1" ]; then
  printversion;
  printhelp;
  exit 0
fi

PARALLEL=""

for opt in $@; do
  if [ "$opt" = "{}" ] ; then
    OPTIONS[${#OPTIONS[*]}]="{}"
    continue;
  fi

  if [[ "${opt:0:1}" != "-" ]] ; then
    FILES[${#FILES[*]}]="$opt"
    continue
  fi
  let START=$START+${#opt}

  if [ "$opt" = "-xp" ] || [ "$opt" = "--xpretend" ]; then
    e="echo"
    continue;
  fi

  if [ "$opt" = "-xh" ] || [ "$opt" = "--xhelp" ]; then
    printversion;
    printhelp;
    exit 0
  elif [ "$opt" = "-xv" ] || [ "$opt" = "--xversion" ]; then
    printversion;
    exit 0
  fi

  if [ "$opt" = "-xf" ] || [ "$opt" = "--xfork" ]; then
    PARALLEL="1";
    continue;
  fi

  OPTIONS[${#OPTIONS[*]}]="$opt"
done

for ((i=0; $i<${#FILES[@]}; i++ )); do
  if [ -f "${FILES[$i]}" ] ; then

    # replace {} with input
    filest=`echo ${FILES[$i]} | sed 's/\(.*\)\..*/\1/g'` #file name with no extnesion

    unset OPTIONS_TMP
    for (( l=0; $l<${#OPTIONS[@]}; l++ )) ; do
      for (( m=0; $m<${#OPTIONS[$l]}; m++ )) ; do

        if [[ "${OPTIONS[$l]:$m:2}" == "{}" ]] ; then
          str_head=${OPTIONS[$l]:0:$m}
          OPTIONS_TMP[$l]="$str_head""$filest"

          let m=$m+1+${#filest}
        else
          OPTIONS_TMP[$l]="${OPTIONS_TMP[$l]}""${OPTIONS[$l]:$m:1}"
        fi
      done
    done

    COMMAND=""

    case "${FILES[$i]}" in
      *.tar.bz2)   COMMAND=(tar xjf) ;;
      *.tar.gz)    COMMAND=(tar xzf) ;;
      *.bz2)       COMMAND=(bunzip2) ;;
      *.rar)       COMMAND=(rar x) ;;
      *.gz)        COMMAND=(gunzip) ;;
      *.tar)       COMMAND=(tar xf) ;;
      *.tbz2)      COMMAND=(tar xjf) ;;
      *.tgz)       COMMAND=(tar xzf) ;;
      *.zip)       COMMAND=(unzip) ;;
      *.Z)         COMMAND=(uncompress) ;;
      *.7z)        COMMAND=(7z x) ;;
      *)           echo "${FILES[$i]}: unknown file type for x" "${OPTIONS_TMP[@]}" ;;
    esac

    if [[ "$PARALLEL" == "1" ]] ; then
      if [[ "$e" == "echo" ]] ; then
        echo $COMMAND "${FILES[$i]}" ${OPTIONS_TMP[@]} \&  &
      else
        $e $COMMAND "${FILES[$i]}" ${OPTIONS_TMP[@]} &
      fi
    else
      $e $COMMAND "${FILES[$i]}" ${OPTIONS_TMP[@]}
    fi
  else
    echo "file doesnt exist: ${FILES[$i]}"
  fi
done


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 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