Recursive cd ..

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


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.


Tags: , , , ,

3 Responses to “Recursive cd ..”

  1. Rob Says:


    I had an issue cutting and pasting that. One of the double quotes has become one of those magical double quotes that isn’t actually a double quote. Worked after that.

  2. xGoat - readline nerdling Says:

    […] was in search of a faster way of typing “cd ../../..” and friends. He wrote a bash function that took a numeric argument. So with his […]

  3. seanohagan Says:

    Exactly what I was looking for! I changed the <= to < though. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: