Some people make a shell for directories they cd to often. Other people set to hold the pathnames of directories they don't want to retype. But both of those methods make you remember directory abbreviations - and make you put new aliases or shell variables in .cshrc or .profile each time you want to add or change one. There's an easier way: the C shell's cdpath shell variable and the CDPATH variable in ksh, bash, and some versions of sh. I'll use the term "cdpath" to talk about all shells.
When you type the command
foo, the shell first tries
to go to the exact pathname
If that doesn't work, and if
foo is a relative pathname,
the shell tries the same command from every directory listed in the cdpath.
(If you use ksh or sh, see the note at the end of this article.)
Let's say that your home directory is /home/lisa and your current
directory is somewhere else.
Let's also say that your cdpath has the directories /home/lisa,
/home/lisa/projects, and /books/troff.
foo command doesn't work in your current
directory, then your shell will try
foo, in that order.
If the shell finds one, it shows the pathname:
cd foo/home/lisa/foo %
Some Bourne shells don't show the directory name.
All shells print an error, though, if they can't find any
So, set your cdpath to a list of the parent directories that contain directories you might want to cd to. Don't list the exact directories - list the. This list goes in your .cshrc or .profile file. For example, lisa's .cshrc could have:
set cdpath=(~ ~/projects /books/troff)
A Bourne shell user would have this in .profile:
CDPATH=:$HOME:$HOME/projects:/books/troff export CDPATH
(If your system doesn't define
NOTE: Note that the Bourne shell CDPATH in the above example starts with a colon (
:)-which, as in the PATH variable, is actually that stands for "the current directory." Both the sh and ksh I tested required that. Without an empty entry, neither sh or ksh would cd into the current directory! (bash seemed to work like csh, though.) You could actually call this a feature. If there's no empty entry in CDPATH, a user has to use
./subdirnameto go to a subdirectory of the current directory.