alias

You are currently browsing articles tagged alias.

I just realized that I spend way to much time taking a full path w/ filename and then deleting the filename portion after pasting into cd. So here’s a little .bash_profile function that you can alias to cd to make a bit smarter.

function _cd {
  if [[ -n $1 ]]; then
    if [[ -L $1 ]]; then
      [[ "`stat -L $1 -c %F`" != 'directory' ]] && CDIR=`dirname $1`
    elif [[ ! -d $1 ]]; then
      CDIR=`dirname $1`
      [[ "$CDIR" == "." ]] && unset CDIR
    fi
  fi
 
  if [[ -n "$CDIR" ]]; then
    cd $CDIR
  else
    cd $1
  fi
  unset CDIR
}
 
alias cd="_cd"

This should allow you to use cd normall but when you do the following it will put you in the directory that httpd.conf is in:


sizzo:~ screen$ cd /usr/local/apache/conf/httpd.conf
sizzo:/usr/local/apache/conf screen$

If you use or expand this please share your experience! There are a couple known issues such as reverse traversing across links from within link directories, not sure if there’s a way around this yet. I’ll update as issues may arise.