Change directory upwards to specified goal

3

1

I'm often deep inside a directory tree, moving upwards and downwards to perform various tasks. Is there anything more efficient than going 'cd ../../../..'?

I was thinking something along the lines of this: If I'm in /foo/bar/baz/qux/quux/corge/grault and want to go to /foo/bar/baz, I want to do something like 'cdto baz'. I can write some bash script for this, but I'd first like to know if it already exists in some form.

Haakon

Posted 2010-06-01T18:49:14.783

Reputation: 133

Answers

2

Here's a function that does what you want:

cdto () { cd "${PWD%/$1/*}/$1"; }

Here's another handy one:

c2 () {
        local path num
    if (($# != 0))
    then
        path='./'
    fi
    if [[ -z ${1//.} ]]
    then
        num=${#1}
    elif [[ -z ${1//[[:digit:]]} ]]
    then
        num=$1
    else
        echo "Invalid argument"
        return 1
    fi
    for ((i=0; i<num-1; i++))
    do
        path+='../'
    done
    cd $path
}

Usage:

c2 .    # same as cd .
c2 ..   # same as cd ..
c2 ...  # same as cd ../..
c2 3    # also same as cd ../..
c2      # same as cd (which is the same as cd ~)

I thought one of the shells used to have the cumulative dot-dot-dot feature (I even checked Vista just now and it didn't have it although Google claims that some versions of Windows do).

Edit

An undocumented feature of Bash is that a lot of characters are acceptable in function names. As a result, you can do this:

.. () { cd ..; }
... () { cd ../..; }
.... () { cd ../../..; }
..... () { cd ../../../..; }

Paused until further notice.

Posted 2010-06-01T18:49:14.783

Reputation: 86 075

Thanks, the small function you posted is exactly what I asked for, except this: how can I use it as a command in the shell? It works inside a shell script, cd in a shell doesn't affect the pwd of the shell that called it. I tried putting it into an alias, which didn't work either. Any suggestions? – Haakon – 2010-06-02T21:18:33.903

1@haakon: Put the function in a file and source the file using . filename. That adds the function to the current environment. You could also simply add the function definition to your ~/.bashrc file or, like I do, put it in a file with other functions called ~/bin/functions then in my ~/.bashrc I have a statement that sources that file . ~/bin/functions. – Paused until further notice. – 2010-06-02T22:52:18.677

3

If you often "go somewhere" and then want to "go back" you could use bash's directory stack: pushd to change to the specific directory and popd to go back where you came from.

[/tmp]$ mkdir -p some/deep/directory/tree
[/tmp]$ pushd some/deep/directory/tree
/tmp/some/deep/directory/tree /tmp

[/tmp/some/deep/directory/tree]$ pushd ..
/tmp/some/deep/directory /tmp/some/deep/directory/tree /tmp

[/tmp/some/deep/directory]$ popd
/tmp/some/deep/directory/tree /tmp

[/tmp/some/deep/directory/tree]$ popd
/tmp

[/tmp]$

Otherwise tweak $CDPATH as suggested by JRobert.

Benjamin Bannier

Posted 2010-06-01T18:49:14.783

Reputation: 13 999

Cool solution! I hadn't seen that before. – Jarvin – 2010-06-01T20:51:01.927

2

Make a CDPATH. It does for 'cd' what PATH does for finding executables. From 'man bash':

CDPATH The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".:~:/usr".

JRobert

Posted 2010-06-01T18:49:14.783

Reputation: 6 128

Thanks, I have used CDPATH in the past but it requires planning ahead and having very static paths you want to go to. It's immensely useful in those cases, though. – Haakon – 2010-06-02T11:07:26.983

0

"cd -" moves back to the directory you were last in.

dan@home:/home/dan/ $ cd test/2009/apt/
dan@home:/home/dan/test/2009/apt/ $ cd -
dan@home:/home/dan/ $ 

Jarvin

Posted 2010-06-01T18:49:14.783

Reputation: 6 712

I use cd - a whole lot; it's good for when you go back and forwards between two directories. But as soon as you have three or more, it breaks. – Haakon – 2010-06-02T11:09:47.150