Section 6.13. Directory Access and the Directory Stack


[Page 237 (continued)]

6.13. Directory Access and the Directory Stack

Bash provides a built-in version of cd that works as shown in Figure 6-42.


[Page 238]

Figure 6-42. Description of the cd shell command.

Shell Command: cd { name }

cd oldName newName

The first form of the cd command is processed as follows:

  • If name is omitted, the shell moves to the home directory specified by $HOME.

  • If name is equal to -, the shell moves to the previous working directory that is kept in $OLDPWD.

  • If name begins with a /, the shell moves to the directory whose full name is name.

  • If name begins with anything else, the shell searches through the directory sequence specified by $CDPATH for a match, and moves the shell to the matching directory. The default value of $CDPATH is null, which causes cd to search only the current directory.

If the second form of cd is used, the shell replaces the first occurrence of the token oldName by the token newName in the current directory's full pathname, and then attempts to change to the new pathname. The shell always stores the full pathname of the current directory in the variable PWD. The current value of $PWD may be displayed by using the built-in command pwd.


Here's an example of cd in action:

$ CDPATH=.:/usr  ...set my CDPATH. $ cd dir1        ...move to "dir1", located under ".". $ pwd /home/glass/dir1 $ cd include     ...move to "include", located in "/usr". $ pwd            ...display the current working dir. /usr/include $ cd -           ...move to my previous directory. $ pwd            ...display the current working dir. /home/glass/dir1 $ _ 


Bash provides a directory stack mechanism allowing you to easily move back and forth between different directories. The entire stack is stored in the string array $DIRSTACK, allowing easy access to any item in the stack from a Bash shell script.

To push the current directory onto the directory stack and cd to a new directory, use the pushd built-in command (Figure 6-43).


[Page 239]

Figure 6-43. Description of the pushd shell command.

Shell command: pushd [-n] [dir]

pushd saves the current directory as the most recent addition to (i.e., on top of) the directory stack. A subsequent popd will retrieve this directory. Then pushd changes directories to the directory specified. If no new directory is specified, the current directory and the top directory on the stack are swapped (i.e., you pop the current top of the stack and cd there and push the directory you were in onto the stack). If the -n argument is present, do not cd to the new directory, simply push the current directory onto the stack.


When it's time to return to a previous directory, use the popd built-in to retrieve previous locations and cd there (Figure 6-44).

Figure 6-44. Description of the popd shell command.

Shell command: popd [-n]

popd retrieves the last directory that was pushed onto the stack and changes directory to that location. The entry is removed from the stack. If the -n argument is present, do not cd to the new directory, simply remove it from the top of the stack.


In addition to contents of the stack being available in the $DIRSTACK shell variable, the dirs built-in command will print (or empty) the contents of the directory stack (Figure 6-45).

Figure 6-45. Description of the dirs shell command.

Shell command: dirs [-cp]

If no arguments are given, dirs simply prints out the contents of the directory stack. The -p option causes the directories to be printed one per line. The -c option causes the directory stack to be cleared.





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

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