9

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?

Stan
  • 1,397
  • 2
    why not just execute cd ../../../.. and be done with it? – Red Cricket Dec 24 '13 at 04:09
  • 8
    You may enjoy pushd and popd or even cd -. – jscott Dec 24 '13 at 04: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 05: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 – hookenz Dec 30 '13 at 02:29

4 Answers4

18

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.

9

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

pragnesh
  • 492
7

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.

Jason
  • 3,941
0

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)

rjmunro
  • 2,331
  • 4
  • 18
  • 22