< Day Day Up > |
9.5. 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 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 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 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.
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
|
< Day Day Up > |