3

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.

4 Answers 4

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 ../../../..; }
2
  • 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
    Jun 2, 2010 at 21:18
  • 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. Jun 2, 2010 at 22:52
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.

1
  • Cool solution! I hadn't seen that before.
    – Jarvin
    Jun 1, 2010 at 20:51
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".

1
  • 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
    Jun 2, 2010 at 11:07
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/ $ 
1
  • 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
    Jun 2, 2010 at 11:09

You must log in to answer this question.

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