Navigate up without cd ..

terminalSometimes navigating up to a higher directory level from a deeper one in the file-system becomes tedious. Specially when you have to start counting the ..s. Here’s a handy script I came across Reddit today (and simplified greatly by one of our readers) that can reduce your directory changing woes.

Add the following to your ~/.bashrc:

up()
{
    for ((i=1; i<=${1:-1}; i++));
    do
        cd ..
    done
}

Usage:

$ up
OR
$ up 1

$ up 2
$ up 6

It should be obvious by now that the parameter to up is the level, up or up 1 being equivalent to cd ..

4 thoughts on “Navigate up without cd ..”

  1. Haha, very neat, although I like smaller code, so I have adapted it to this:

    up(){
    for ((i=1; i<=${1:-1}; i++)); do
    local d=../$d
    done
    cd $d
    }

    Or even smaller with multiple cds:

    up(){
    for ((i=1; i<=${1:-1}; i++)); do
    cd ..
    done
    }

    It also works if you just run `up' instead of `up 1'.

    1. I love your second version and replaced the original one with it. It’s definitely better. We don’t need all those temp vars and checks for this triviality ;).

      1. I am also using the second version. I just came up with it while writing my comment 😉

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s