romain.bardou.fr - blog RSS

cd Shortcuts Using CDPATH And Symbolic Links

CDPATH is a very convenient feature, yet it is ignored by many Linux users. It is similar to PATH, but for the cd command instead of the "run this program" command. It allows to type, say:

cd project

instead of:

cd /home/romain/programming/ocaml/project

even if the current directory is not /home/romain/programming/ocaml.

How to Use

Add the /home/romain/programming/ocaml/ directory to the CDPATH environment variable:

export CDPATH=$CDPATH:/home/romain/programming/ocaml/

The cd command will now "see" all subdirectories of /home/romain/programming/ocaml/ even when it is not the current directory.

Note that just like PATH you may put several directories, separated by colon (:) characters. The command above appends our new directory at the end of the current value of CDPATH.

Add this to your initialization script (like ~/.bashrc or ~/.zshrc) to make it available all the time.

Using Symbolic Links

To make it even easier, I have only one directory in my CDPATH, namely:

/home/romain/cdpath

which contains symbolic links to other directories, such as:

p -> /home/romain/programming/ocaml/project
m -> /home/romain/data/music

I just type cd p or cd m, from anywhere, to go to the linked directory.

To set this up, create a cdpath directory (you may give it any other name):

mkdir ~/cdpath

Add this directory to your CDPATH variable:

export CDPATH=$CDPATH:~/cdpath

Then, add symbolic links:

cd cdpath
ln -s /home/romain/programming/ocaml/project p
ln -s /home/romain/data/music m

This method has several advantages. You can use the same export CDPATH command on all your computers. You only have to modify your initialization script once and for all: to add new directory shortcuts, you just add a symbolic link in the ~/cdpath directory. You control exactly which directories are usable this way.

The main drawback is that you have to add all directories one by one. If you want a shortcut to all subdirectories of, say, /home/romain/programming/ocaml/ it is simpler to edit your initialization script to add this directory to your CDPATH. Another option is to add a link from, say, o to /home/romain/programming/ocaml/ in the ~/cdpath directory, and then type cd o/project.

cd -

The cd - command changes the current directory to the directory which was the current directory before the current one, if that makes sense. It is similar to the "go back" button of your browser, except that it only stores one directory in its history so if you do it twice it goes back to where you were at the beginning.

cd - is very useful when using CDPATH. Say you are in directory /etc/var and you type cd m, you are now in /home/romain/data/music. You cannot type cd .. to go back to /etc/var, as cd .. would lead you to /home/romain/data instead. However, if you type cd - you will go back to /etc/var. If you type cd - again you will then go back to /home/romain/data/music.

The way cd - works is very simple: it goes to the directory which is contained in the OLDPWD environment variable. You can see its current value:

echo $OLDPWD

Each time you run cd, the OLDPWD environment variable is set to the current directory before cd is run.

The cdpathas Shell Script

If you use the ~/cdpath with symbolic links method, after a while it will get a bit annoying to have to type the ln command to add new links. Usually when I realize that it would be nice to add a link, I am already in the directory to link. So I wrote a very simple script which adds a link from ~/cdpath to the current directory. It's called cdpathas ("CDPATH as") and I use it like this:

cdpathas x

This creates a link from ~/cdpath/x to the current directory. Here is the script:

#!/bin/sh

echo "ln -s $PWD ~/cdpath/$1"
ln -s $PWD ~/cdpath/$1

Save this as a file named cdpathas and give it execution permissions:

chmod +x cdpathas

Put this file in a directory of your PATH, for instance in /usr/local/bin.