My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://www.mattdturner.com/wordpress
and update your bookmarks.

AdSense

Pages

Monday, April 25, 2011

Useful .bashrc Functions

For those of you that read my other post about useful .cshrc functions, you know that I like to automate as many processes as possible.  If there is something redundant that I do frequently, I will script it or make a function to take care of it.

In an effort to assist those of you out there that like to automate processes but use bash instead of C-Shell, I have compiled a list of some useful functions to stick in your .bashrc file.  Here is a list of the functions that this post will contain:

  1. Extract:  This function will determine the type of archive you are dealing with, and issue the corresponding command to extract it.
  2. Compress:  This function will create a .tar.gz archive of whatever files you give.
  3. Swap:  This function will swap the names of 2 different files.
  4. Clock:  This is just a random function that will display a digital clock in your terminal window and update every second.
  5. Cpg:  This function will copy a file to a directory, and also cd you to a directory.
  6. Mvg:  This function works like Cpg, but moves the file instead of copying it.
  7. Findit:  This function will search for a file with the given pattern in the name.

Function Extract


function extract()      # Handy Extract Program.
{
     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
}
Use: extract archive.tar.gz

Function Compress


function compress()  # create .tar.gz archive
{
    tar -cvzf $1 $2
}
Use: compress archive.tar.gz ~/Desktop/*

Function Swap


function swap()  # Swap 2 filenames around, if they exist
{                #(from Uzi's bashrc).
    local TMPFILE=tmp.$$

    [ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
    [ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
    [ ! -e $2 ] && echo "swap: $2 does not exist" && return 1

    mv "$1" $TMPFILE
    mv "$2" "$1"
    mv $TMPFILE "$2"
}
Use: swap file1.txt file2.txt

Function Clock


# clock - a little clock that appeares in the terminal window.
# Usage: clock.
#
clock ()
{
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done
}
Use: clock

Function Cpg


#copy and go to dir
cpg (){
  if [ -d "$2" ];then
    cp $1 $2 && cd $2
  else
    cp $1 $2
  fi
}
Use: cpg file.txt /path/to/dir

Function Mvg


#move and go to dir
mvg (){
  if [ -d "$2" ];then
    mv $1 $2 && cd $2
  else
    mv $1 $2
  fi
}
Use: mvg file.txt /path/to/dir

Function Findit


# find a file with pattern in name
findit (){
   if [ -z ${1} ];then
      echo "Please pass an argument that you want to search for"
   else
      find . -iname "*$1*" -print
   fi
}
Use: findit stringToSearchFor
Note:  findit function is NOT case-sensitive

Feel free to modify any of the functions to your liking.

Have a function that you would like to share?  Post it in the comments!

0 comments:

Post a Comment