Directory Stacks

Introduction

Your shell maintains a stack of directories to help you move among directories.

Notes

  • The pushd command is like the cd command but your current directory is pushed onto the stack before moving to the new directory.
  • The popd command will move you back to the directory on the top of the stack.
  • The dirs command will list the directory stack:
    
    % pwd
    /u4/visitors/csgst00
    % pushd ~kensmith
    ~kensmith ~
    % ls
    Accounts               calspan                system_admin
    Mail                   cs391                  tmp
    News                   priv                   work
    bin                    scripts
    % pwd
    /u0/csstaff/kensmith
    % popd
    ~
    % pwd
    /u4/visitors/csgst00 
    
    
  • pushd with no arguments moves you to the directory on the top of the stack and moves your current directory onto the stack:
    
    % pwd
    /u4/visitors/csgst00
    % pushd ~kensmith
    ~kensmith ~
    % pushd /usr/local/bin
    /usr/local/bin ~kensmith ~
    % dirs
    /usr/local/bin ~kensmith ~
    % pushd
    ~kensmith /usr/local/bin ~
    % popd
    /usr/local/bin ~
    % popd
    ~
    % 
    
    
  • You can use the directory stack for command line substitutions.
  • dirs -v lists directories on the stack with numbers for each directory.
  • You can use =n substitution to access those names:
    
    % pushd ~kensmith
    ~kensmith ~
    % pushd /usr/local/bin
    /usr/local/bin ~kensmith ~
    % dirs -v
    0       /usr/local/bin
    1       ~kensmith
    2       ~
    % ls =1
    Accounts               calspan                system_admin
    Mail                   cs391                  tmp
    News                   priv                   work
    bin                    scripts
    %