Using cd to go up multiple directory levels

I'm dealing with java projects which often result in deeply nested folders (/path/to/project/com/java/lang/whatever, etc) and sometimes want to be able to jump, say, 4 directory levels upwards. Typing cd ../../../.. is a pain, and I don't want to symlink. Is there some flag to cd that lets you go up multiple directory levels (in my head, it would be something like cd -u 4)? Unfortunately I can't find any man page for cd specifically, instead just getting the useless "builtins" page.


Solution 1:

A simple function, with an alias, too:

function cd_up() {
  cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'

(You could define this in ~/.bashrc if you want it in every instance).

It's simple to use:

$ cd.. 10

Solution 2:

Or... try this: (yay Google)

Navigate up the directory using ..n :

In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level.

Add the following alias to the .bash_profile and re-login.

alias ..="cd .."

alias ..2="cd ../.."

alias ..3="cd ../../.."

(etc)

See Hack #2