//Tips tagged cd
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
You often see people using aliases like "alias ..='cd ..'" and "alias ...='cd ../..'". Here is a general version of this kind of '..' command, which takes an argument for how many levels up to go.

# .. - Does a 'cd ..'
# .. 3 - Does a 'cd ../../..'
#
function .. (){
  local arg=${1:-1};
  while [ $arg -gt 0 ]; do
    cd .. >&/dev/null;
    arg=$(($arg - 1));
  done
}


View Comments »



This can be used instead of "mv" to move file(s) and then cd into the destination folder.

mvf() {
if  mv "$@"; then
  shift $(($#-1))
  if [ -d $1 ]; then
    cd ${1}
  else
    cd `dirname ${1}`
  fi
fi
}


View Comments »

Tip 550 gives a script for a '..' command which will go up n directory levels, however the version there messes up your previous directory so "cd -" will not work. The version below fixes this problem:

.. v1.1
Usage .. [n]
Go up n-levels.
i.e.: .. 3 will go up 3 levels

function .. (){
  local arg=${1:-1};
  local dir=""
  while [ $arg -gt 0 ]; do
    dir="../$dir"
    arg=$(($arg - 1));
  done
  cd $dir >&/dev/null
}


Another possible task is to go up several levels until we find the directory we need. The function below will do this:

... v0.1
Usage ... Thing/Some
Go up until you encounter Thing/Some, then go there
i.e.: I'm in /usr/share/X11
... src will go up to /usr, then change to /usr/src

function ... (){
  if [ -z "$1" ]; then
    return
  fi
  local maxlvl=16
  local dir=$1
  while [ $maxlvl -gt 0 ]; do
      dir="../$dir"
      maxlvl=$(($maxlvl - 1));
      if [ -d "$dir" ]; then 
        cd $dir >&/dev/null
      fi
  done
}


View Comments »




Home Latest Browse Top 25 Random Hall Of Fame Contact Submit