Previous Section  < Day Day Up >  Next Section

9.5. Manipulating the Directory Stack

If you find that as you work, you cd up and down the directory tree into many of the same directories, you can make it easy to access those directories by pushing them onto a directory stack and manipulating the stack. The directory stack is often compared to stacking trays in a cafeteria where the trays are stacked on top of each other, the first one being at the bottom of the stack. The pushd built-in command pushes directories onto a stack and the popd command removes them. (See following examples.) The stack is a numbered list of directories with the top directory being the most recent directory pushed onto the stack. The directories are numbered starting with the top directory at 0, the next one numbered 1, and so on. The built-in command, dirs, with a –v option, displays the numbered directory stack.

9.5.1 The pushd and popd Commands

The pushd command with a directory as an argument causes the new directory to be added to the directory stack and, at the same time, changes to that directory. If the argument is a dash (), the dash refers to the previous working directory. If the argument is a + and a number (n), pushd extracts the nth directory from the stack and pushes it onto the top, then changes to that directory. Without arguments, pushd exchanges the top two elements of the directory stack, making it easy to switch back and forth between directories. There are a number of shell variables that control the way pushd works. (See "Setting Local Variables" on page 441.)

To save a directory stack across login sessions, you must set the savedirs variable in one of the tcsh initialization files (e.g., ~/.tcshrc). The directory stack will be stored in a file called ~/.cshdirs and will be automatically sourced when the shell starts up.

The popd command removes a directory from the top of the stack, and changes to that directory.

See Table 9.1 for a list of directory stack variables.

Table 9.1. Directory Stack Variables

Variable

What It Does

deextract

If set, pushd +n extracts the nth directory from the directory stack before pushing it onto the stack.

dirsfile

Can be assigned a filename where the directory stack can be saved across logins.

dirstack

Used to display the stack or assign directories to it.

dunique

Before pushing a directory onto the stack, removes any directories with the same name.

pushdsilent

Doesn't print the directory stack when pushd is executed.

pushdtohome

If set, pushd without arguments, is same as pushd ~ or cd.

pushtohome

Without arguments, pushes to ~, the user's home directory.

savedirs

Saves the directory stack across logins.


Example 9.24.

1   % pwd

    /home/ellie



    % pushd ..

    /home ~



    % pwd

    /home



2   % pushd       # swap the two top directories on the stack

    ~ /home



    % pwd

    /home/ellie



3   % pushd perlclass

    ~/perlclass  ~  /home



4   % dirs -v

    0      ~/perlclass

    1      ~

    2      /home



5   % popd

    ~      /home

    % pwd

    /home/ellie



6   % popd

    /home



    % pwd

    /home



7   % popd

    popd: Directory stack empty.


EXPLANATION

  1. First the pwd command displays the present working directory, /home/ellie. Next the pushd command with .. as its argument pushes the parent directory (..) onto the directory stack. The output of pushd indicates that /home is at the top of the directory stack and the user's home directory (~), /home/ellie, is at the bottom of the stack. pushd also changes the directory to the one that was pushed onto the stack; that is, .. , which translates to /home. The new directory is displayed with the second pwd command.

  2. pushd, without arguments, exchanges the two top directory entries on the stack and changes to the swapped directory; in this example, the directory is switched back to the user's home directory, /home/ellie.

  3. The pushd command will push its argument, ~/perlclass, onto the stack, and change to that directory.

  4. The built-in dirs command displays the numbered directory stack, with 0 being the top level.

  5. The popd command removes a directory from the top of the stack, and changes to that directory.

  6. The popd command removes another directory from the top of the stack, and changes to that directory.

  7. The popd command cannot remove any more directory entries because the stack is empty, and issues an error message saying so.

    Previous Section  < Day Day Up >  Next Section