Posts Tagged ‘cd’

Recursive cd ..

December 23, 2008

There are several projects i am currently working on that have deep directory trees. For example:

/home/tom/project/sr/svn.studentrobotics.org/slug/init-br/build_armeb/staging_dir/armeb-linux-uclibc/include/linux/spi

Ive found my self getting increasingly irritated by having to type variations on :

cd ../../../../..

So set about writing an alias to simplify this. After a little investigation, I discovered Bash functions and came up with this:

xs () { if [[ -z "${1}" ]]; then  cd ..; elif [ ! $(echo "$1" | grep -E "^[0-9]+$”) ]; then  echo ARG1 NAN; else for ((i=0;i<=$1 ;i+=1)); do  cd ..; done; fi }

By adding this line to your .bashrc file (or similar) the command xs (note its next to cd) allows one to travel up the directory tree, examples:

xs 5       -> cd ../../../../..

xs 2       -> cd ../..

xs         -> cd ..

Hope this is usefull to someone.