Module 14 cd

Previous Table of Contents Next


Module 14
cd

DESCRIPTION

The internal cd command changes your present working directory. There are two forms of the change directory command. The first changes you to a new destination based on the single argument given. The second performs a string substitute on the current path and then changes directories. The cd command lets you do the following:

   Change working directories
   Change to last previous working directory
   Use a string substitution in the current path to change to a similar directory path
   Use tilde substitution to change to $HOME/subdirectories
   Use tilde substitution to change to another user s $HOME/subdirectories

The tilde substitution, - option, path substitution, and CDPATH does not apply to the sh .

COMMAND FORMAT

Following is the general format of the cd command.

 cd ~      cd [ directory ]      cd old new      chdir ~      chdir [ directory ]      chdir old new 

Options

The following option may be used to control how cd functions.

- The present working directory is changed to the previous working directory. The shell maintains an OLDPWD variable to know the previous directory path.

Arguments

The following list describes the arguments that may be passed to the cd command.

directory Specifies the new present working directory. The default is $HOME if you do not specify a directory. See tilde substitution in the following section.
old The old string searched for in the current working directory s path that is changed to the new string.
new The new string that replaces the old string in the current working directory s path.

FURTHER DISCUSSION

Using the first form of cd , you may use an absolute path or a relative path. An absolute path begins with a / (slash), a relative path begins with a directory name or a . (dot). Either form can be used to achieve the same result. Your decision should be the shortest path to reach your destination or the path syntax you understand best. The following examples help clarify the use of absolute and relative paths. Both commands will place you in the same directory from your HOME directory. Assume your current directory is /ul/ts/mylogin.

 cj> cd /u1/ts/nancy   # absolute path to user nancy      cj> cd                # return to $HOME      cj> cd ../nancy       # relative path to user nancy 

If no arguments are given, cd assumes the HOME variable is the desired directory.

 cj> cd                # same as cd $HOME 

If the - (hyphen) option is present, cd changes to the previous directory.

 cj> cd -              # same as cd $OLDPWD, back to ../nancy 

Tilde Substitution

If the ~ (tilde) option is present, cd places your HOME path in front of any path you provide after the ~. If the ~ is followed by a string, then the shell searches the /etc/passwd file for a matching user name. If one is found, that user s HOME path is used. For example:

 cj> cd ~/db           # cd to $HOME/db      cj> cd ~nancy/bin     # cd /u1/dvlp/nancy/bin 

The first example uses your HOME directory($HOME/db), whereas the second uses nancy s HOME directory (field 5 of /etc/passwd).

CDPATH

If a single directory name is specified, cd searches the directories listed in the CDPATH variable. Each directory listed in CDPATH is searched for a subdirectory that matches the specified argument. The first match is used as the new directory.

The CDPATH variable is a colon -separated list of directory paths that cd searches for a matching subdirectory. The default CDPATH is null, specifying the present working directory. Therefore, if you do not have CDPATH set, cd will search only the current directory for the directory name given as an argument. If you set CDPATH=:/usr:/usr/spool, cd will look for the requested directory in your HOME directory first, then check the /usr directory, and then search /usr/spool. For example:


CAUTION:    
The current directory must be the first path defined in the CDPATH variable. The current directory is defined as CDPATH=: or CDPATH=.:. If you do not specify the current directory first, cd will complain about not being able to find a directory in the current directory if the directory is not specified in the CDPATH variable.

 cj> CDPATH=:/usr:/usr/spool      cj> export CDPATH      cj> cd bin      cj> pwd      /usr/bin 

would cd you to the /usr/bin directory. To cd to your db directory, you would just type

 cj> cd db      cj> pwd      /ul/ts/mylogin/db 

Your new present working directory would be /u1/ts/mylogin/db.

The second form of the cd command is to have a string of the current path substituted with a new string. The following scenario reveals how cd handles substitutions within paths. You may want to create these directories and then try these commands. Type mkdir C/src sh/src C/src/shtools sh/src/shtools and press Return.

 cj> cd C/src/shtools      cj> pwd      /ul/ts/mylogin/C/src/shtools      cj> cd C sh                # substitute sh in place of C      cj> pwd      /ul/ts/mylogin/sh/src/shtools 

The substitution command is useful to switch between directories sharing almost the exact same long path names .


NOTE:    
The cd command cannot be executed from a restricted shell.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

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