13.7. Manipulating the Directory Stack

 <  Day Day Up  >  

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 pushd built-in command pushes directories onto a stack and the popd command removes them. (See Example 13.40.) The stack is a list of directories with the directory at the left being the most recent directory pushed onto the stack. The directories can be listed with the built-in dirs command.

13.7.1 The dirs Built-In Command

The built-in command dirs , with a “l option, displays the directory stack with each of its directories in full pathname format; without an option, dirs uses a tilde to denote the home directory. With a +n option, dirs displays the n th directory entry counting from the left in the directory list, starting at 0. With the “n option, it does the same thing, but starts at the right-hand side of the directory list with 0.

13.7.2 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 +n where n is a number, pushd rotates the stack so that the n th directory from the stack, starting at the left-hand side, is pushed onto the top of the stack. With a “n option, it does the same thing but starts at the right-hand side. Without arguments, pushd exchanges the top two elements of the directory stack, making it easy to switch back and forth between directories.

The popd command removes a directory from the top of the stack, and changes to that directory. With +n , where n is a number, popd removes the n th entry, starting at the left of the list shown by dirs .

Example 13.40.
 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   ~/perlclass ~ /home  5   $  dirs -l   /home/ellie/perlclass /home/ellie  /home  6   $  popd   ~/home  $  pwd   /home/ellie  7   $  popd   /home  $  pwd   /home  8   $  popd   bash: 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 (starting at the left-hand side of the displayed list) and the user 's home directory, represented by the tilde character ( ~ ) 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. The pushd command, 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 directory stack, with the top level starting at left-hand side of the listing. The tilde expands to the user's home directory.

  5. With the “l option, dirs list displays the directory stack with full pathnames instead of using tilde expansion.

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

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

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

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

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