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?

5
  • 2
    why not just execute cd ../../../.. and be done with it? Dec 24, 2013 at 4:09
  • 8
    You may enjoy pushd and popd or even cd -.
    – jscott
    Dec 24, 2013 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. Dec 24, 2013 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, 2013 at 13:21
  • Open another terminal window and don't type cd ../../../.. at all
    – hookenz
    Dec 30, 2013 at 2:29

4 Answers 4

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

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.

1
  • 3
    Yes, or function cdd () { cd ../../../../$1/bar/dest ; }, so then you can just type e.g. cdd foo, cdd foo2. Dec 24, 2013 at 6:39
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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .