.bashrc -- extract compressed files easier.

For .bashrc information and for posting your interesting/useful aliases. Also post other dotfile info here.

.bashrc -- extract compressed files easier.

Postby crouse » Mon Aug 21, 2006 11:47 pm

I found this one on the arch linux forums --- kind of a cool use of the .bashrc file ;)

Code: Select all
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
}
User avatar
crouse
Site Admin
 
Posts: 562
Joined: Sun May 15, 2005 9:36 pm
Location: Des Moines, Iowa

Postby jbsnake » Tue Aug 22, 2006 2:34 am

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:
jbsnake
Site Admin
 
Posts: 248
Joined: Tue May 17, 2005 7:31 pm
Location: Georgia

Postby coastie » Tue Aug 22, 2006 6:18 am

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

Postby jeo » Tue Aug 22, 2006 11:11 am

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
jeo
 
Posts: 142
Joined: Wed May 03, 2006 2:05 pm

Postby crouse » Sat Sep 02, 2006 11:13 pm

Code: Select all
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
User avatar
crouse
Site Admin
 
Posts: 562
Joined: Sun May 15, 2005 9:36 pm
Location: Des Moines, Iowa

Postby Daenyth » Tue Apr 01, 2008 11:19 am

A bit cleaner:
Code: Select all
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?
Daenyth
 
Posts: 49
Joined: Tue Apr 01, 2008 10:19 am

Re: .bashrc -- extract compressed files easier.

Postby caibbor » Fri Mar 12, 2010 2:40 pm

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: Select all
#!/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
caibbor
 
Posts: 22
Joined: Wed Mar 10, 2010 8:05 pm


Return to Aliases and Dotfiles

Who is online

Users browsing this forum: No registered users and 1 guest

cron