Sunday, 13 April 2014

Useful bashrc setup for command line efficiency



I find the following config items in my .bashrc file to use any system efficiency when I set it up.

Include home bin within PATH


  • I create a bin directory in the home directory to add in all my executable programs

# Include bin dir to PATH
export PATH=/home/cwishaw/bin:$PATH


Password Generator

 

  • I find it useful to be able to generate random passwords quickly :

GeneratePwd(){ < /dev/urandom tr -dc A-Z-a-z-0-9 | head -c${1:-12};echo;}
  • This will generate a random password of 12 chacacters :
$ GeneratePwd
JTRKjlXyAIGS

 

Extract files quickly


  • The following command will extract the most common file types without having to memorize all the syntax
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 "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
 }



Move up directories


  • The following command enabled you to quickly move up directories

Up(){
  local d=""
  limit=$1
  for ((i=1 ; i <= limit ; i++))
    do
      d=$d/..
    done
  d=$(echo $d | sed 's/^\///')
  if [ -z "$d" ]; then
    d=..
  fi
  cd $d
}


Include bash_aliases if exists

 

  • I prefer to manage all my aliases in a separate file as its easier to maintain.

if [ -f ~/.bash_aliases ];
 then
        . ~/.bash_aliases
fi


  •  The bash_aliases can have all your alias definitions of the format :

alias pacman='sudo pacman'
alias vi='vim'




No comments:

Post a Comment