Say if I need to do this a lot:

cd ../../../../foo/sub1/bar/dest/
cd ../../../../foo2/sub1/bar/dest/
cd ../../../../foo3/sub1/bar/dest/
cd ../../../../foo/sub1/bar/dest/

Is there any faster way to not always typing ../../../../ so many times? Any creative ideas?

  • 2
    why not just execute cd ../../../.. and be done with it? – Red Cricket Dec 24 '13 at 4:09
  • 8
    You may enjoy pushd and popd or even cd -. – jscott Dec 24 '13 at 4:28
  • 1
    If you're going that far back it might make sense to just cd /actual/path/you/want depending on how deep the folders go. – Drew Khoury Dec 24 '13 at 5:06
  • This is a lot more effort than it's worth. If you're moving between directories, full path specification or just using cd - (to go to last directory) is easier. I typically alias h="history|grep" so that I can use the history entry shortcuts instead. Something like h foo3, followed by !450 to repeat that command. – ewwhite Dec 25 '13 at 13:21
  • Open another terminal window and don't type cd ../../../.. at all – Matt Dec 30 '13 at 2:29

The classic way of doing this is setting the CDPATH variable. In your case it could be set to .:../../../../ or .:/path/to/the/parent/of/foo to achieve the desired effect. You can then simply do cd foo/sub1/bar/dest and get to where you want.

The advantage of this is that tab completion for cd also looks at $CDPATH and will magically work.


You can use autojump which automatically learn from previous cd command and you only need to specify unique string from full path next time. ex. j foo, j foo1, j foo2


You can create shortcuts for these commands by adding aliases into the bash configuration file (~/.bashrc or /etc/bash.bashrc)

alias cdd="cd ../../../../"

Then when you type cdd it will execute that command.

  • 3
    Yes, or function cdd () { cd ../../../../$1/bar/dest ; }, so then you can just type e.g. cdd foo, cdd foo2. – Andrew Schulman Dec 24 '13 at 6:39

I recently came across https://github.com/rupa/z, which lets you jump to a recently used directory by regex. So you could do something like:

z foo3.*dest

instead of:

cd ../../../../foo3/sub1/bar/dest/

(as long as you have visited that directory before, and nothing more recent / frequently used matches that regex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.