Section 8.13. The Directory Stack


[Page 330]

8.13. The Directory Stack

The C shell allows you to create and manipulate a directory stack, which makes life a little easier when you're flipping back and forth between a small working set of directories. To push a directory onto the directory stack, use the pushd command (Figure 8-40).

Figure 8-40. Description of the pushd shell command.

Shell Command: pushd [+number | name]

pushd pushes the specified directory onto the directory stack, and works like this:

  • When name is supplied, the current working directory is pushed onto the stack and the shell moves to the named directory.

  • When no arguments are supplied, the top two elements of the directory stack are swapped.

  • When number is supplied, the numberth element of the directory stack is rotated to the top of the stack and becomes the current working directory. The elements of the stack are numbered in ascending order, with the top as number 0.


To pop a directory from the directory stack, use the popd command (Figure 8-41).

Figure 8-41. Description of the popd shell command.

Shell Command: popd [+number]

popd pops a directory from the directory stack, and works like this:

  • When no argument is supplied, the shell moves to the directory that's on the top of the directory stack and then pops it.

  • When a number is supplied, the shell moves to the numberth directory on the stack and discards it.


The dirs command (Figure 8-42) lets you see the contents of the directory stack.

Figure 8-42. Description of the dirs shell command.

Shell Command: dirs

dirs lists the current directory stack.


Here are some examples of directory stack manipulation:

% pwd            ...I'm in my home directory. /home/glass % pushd /        ...go to root directory, push home dir. / ~              ...displays directory stack automatically. 
[Page 331]
% pushd /usr/include ...push another directory. /usr/include / ~ % pushd ...swap two stack elements, go back to root. / /usr/include ~ % pushd ...swap them again, go back to "/usr/include". /usr/include / ~ % popd ...pop a directory, go back to root. / ~ % popd ...pop a directory, go back to home. ~ % _


8.13.1. The Hash Table

As described in Chapter 5, "The Linux Shells," the PATH variable is used when searching for an executable file. To speed up this process, the C shell stores an internal data structure, called a hash table, that allows the directory hierarchy to be searched more quickly. The hash table is constructed automatically whenever the ".tcshrc" file is read. In order for the hash table to work correctly, however, it must be reconstructed whenever $PATH is changed, or whenever a new executable file is added to any directory in the $PATH sequence. The C shell takes care of the first case automatically, but you must take care of the second.

If you add or rename an executable in any of the directories in the $PATH sequence except your current directory, you should use the rehash command to instruct the C shell to reconstruct the hash table. If you wish, you may use the unhash command to disable the hash table facility, thereby slowing down the search process. Figure 8-43 describes how these shell commands work.

Figure 8-43. Description of the rehash and unhash shell commands.

Shell Command: rehash

unhash

The rehash shell built-in command is used to rebuild your shell's command search hash list.

The unhash shell built-in command is used to disable search path hashing.


In the following example, I added a new executable into the directory "~/bin", which was in my search path. The shell couldn't find it until I performed a rehash.

% pwd                        ...I'm in my home directory. /home/glass % echo $PATH                 ...list my PATH variable. .:/home/glass/bin:/usr/bin:/usr/local/bin:/bin: % cat > bin/script.csh        ...create a new script. 
[Page 332]
# echo found the script ^D ...end-of-input. % chmod +x bin/script.csh ...make executable. % script.csh ...try to run it. script.csh: Command not found. % rehash ...make the shell rehash. % script.csh ...try to run it again. found the script ...success! % _





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net