Letting the Shell Find Directories for You

The directory stack makes it easier to revisit a directory after you have been there, but you still have to type the directory's name the first time you enter it. The shell can make that easier to do, by reducing the need to type multiple component pathnames to get to the directories you want. The shell accomplishes this feat by allowing you to set up a directory search path, a mechanism analogous to the command search path.

When you run a command, you usually type only the command's basename (the last component of its full pathname). The shell finds the command by looking in the directories named in your path variable. In similar fashion, if you set the cdpath shell variable, the shell understands its value to be a list of locations in which to look when you give a directory argument to cd or pushd. In other words, if you enter cd dir or pushd dir and the shell doesn't find dir in your current directory, it will search the directories named in cdpath.[23]

Simple directory tree

Figure 13-1. Simple directory tree

Suppose the directory layout for your account is as shown in Figure 13-1, and you want to successively change into the Programs, Shell, Mail, and tokenscan directories. If you didn't set cdpath, you would have to type out pathnames as follows:

% cd ˜/Projects/Programs
% cd ˜/Projects/Books/Shell
% cd ˜/Mail
% cd ˜/Projects/Programs/tokenscan

Much of ...

Get Using csh & tcsh now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.