UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.4 How Does UNIX Find Your Current Directory? Chapter 14
Moving Around in a Hurry
Next: 14.6 The Shells' pushd and popd Commands
 

14.5 Saving Time When You Change Directories: cdpath

Some people make a shell alias (10.2) for directories they cd to often. Other people set shell variables (6.8) 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 cd foo, the shell first tries to go to the exact pathname foo. 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. If your cd foo command doesn't work in your current directory, then your shell will try cd /home/lisa/foo, cd /home/lisa/projects/foo, and cd /books/troff/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 foo directory.

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 parent directories (1.21). 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 $HOME, try $LOGDIR.)

NOTE: Note that the Bourne shell CDPATH in the above example starts with a colon (:)-which, as in the PATH variable, is actually an empty entry (6.4) 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 cd ./subdirname to go to a subdirectory of the current directory.

- JP


Previous: 14.4 How Does UNIX Find Your Current Directory? UNIX Power ToolsNext: 14.6 The Shells' pushd and popd Commands
14.4 How Does UNIX Find Your Current Directory? Book Index14.6 The Shells' pushd and popd Commands

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System