< Day Day Up > |
13.7. Manipulating the Directory StackIf 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 CommandThe 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 nth 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 CommandsThe 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 nth 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 nth 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
|
< Day Day Up > |