climb up the directory tree faster

if I'm in a very deep directory a/b/c/d/e/f/g/h/i/j and want to come back a/b/c, I have to use ../../../../../../../.

Is there command I can pass through a number, e.g. cd up 7, to speed this operation up?


Solution 1:

You could write a function like this:

up() {
    local path i
    for (( i=0; i < $1; i++ )); do
        path+=../
    done
    cd "$path"
}

Put that in your ~/.bashrc, then you can run e.g. up 7 to go up 7 directories. You could override cd to allow cd up 7 too, but just making a new command is shorter and less hassle.

Solution 2:

If you are toggling between 2 directories, you can use cd - to switch between both.

If you want to bookmark a few directories that you would probably cd to often, use pushd and popd -> google for more information.

Or, if you know you have to cd to 7th grand parent very often, you could create an alias, like:

alias cd7up='cd ../../../../../../../'