Previous Table of Contents Next

Module 110
pushd (csh)


The internal pushd command pushes directories around on the directory stack. The directory stack contains a list of directories you can change by using the pushd command. Each time a pushd command is executed, the shell cds (changes directory) to the top directory entry on the stack. The stack entries are numbered from the top to the bottom starting at one. When the stack is displayed (after each pushd) the top is on the left.

If no argument is given, the top two directories on the stack are exchanged. The new top directory becomes the new present working directory.

If a new directory path is provided as an argument, it is pushed onto the top of the directory stack. The present working directory is changed to the new top directory.

If a +n is given as an argument, the stack is rotated so the nth entry becomes the top entry. For example, if the directory stack looked like this

  /u1/ts/mylogin/d1 /u1/ts/mylogin/d2 /u1/ts/mylogin/d3/u1/ts/mylogin/d4

and you typed

  pushd +2

the stack would change to

  /u1/ts/mylogin/d3 /u1/ts/mylogin/d4 /u1/ts/mylogin/d1/u1/ts/mylogin/d2

and your present working directory would be /u1/ts/mylogin/d3.


The format of the pushd command follows.

  pushd [ +n ]
  pushd [ directory ]


Only one option may be used to control how pushd functions.

+n Rotate the nth entry in the directory stack to the top of the stack. All entries preceding the specified entry are moved to the bottom of the stack. The top stack entry becomes the new present working directory.


Only one argument may be passed to the pushd command.

directory The directory path to add to the top of the directory stack. The shell then changes directory to this new entry.
If no arguments are given, the top two directories on the directory stack are exchanged and the shell changes directories to the new top directory.

The ksh implements pushd with functions. Check with your system administrator to see if pushd is available.

TIP:  Most users of the csh alias the pushd, popd, and dirs commands. For example, you might want to add the following lines to your .cshrc file.

     alias u pushd  # use u instead of pushd
     alias o popd   # use o instead of popd
     alias d dirs   # use d instead of dirs


Refer to the dirs and popd commands described in modules 36 and 104.


The pushd command is useful when you are changing directories often. If you cd from one directory to another and back again, pushd can alleviate a large amount of typing. You simply type u directory instead of cd directory when you want to change directories. Then when you want to return to a previous directory you use the dirs command to display the directory stack. Select the desired directory and then type u +n to change directories to it.


In this activity you use the pushd command to create a directory stack and change directories. Begin at the shell prompt.

1.  Type pushd /usr/lib and press Return. You are now in the system's lib directory.
2.  Type pushd /usr/spool/uucppublic and press Return. Notice you have changed directory to the uucppublic directory. Try the pwd command to check.
3.  Type pushd ~ and press Return. Now you are in your HOME directory.
4.  To return to the lib directory simply type pushd +2 and press Return.
5.  To return to the uucppublic directory type pushd +3 and press Return.
6.  Type pushd +2 and press Return to return to your HOME directory.
7.  Turn to Module 104 to continue the learning sequence.

Previous Table of Contents Next