Previous Table of Contents Next

Module 14


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.


Following is the general format of the cd command.

     cd ~
     cd [ directory ]
     cd old new

     chdir ~
     chdir [ directory ]
     chdir old new


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.


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.


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).


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:

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

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

     cj> cd db
     cj> pwd

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
     cj> cd C sh                # substitute sh in place of C
     cj> pwd

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

The cd command cannot be executed from a restricted shell.


The following message may be returned if cd cannot perform the requested directory change.

"directory" bad directory The directory is not readable and executable by the requesting user or it does not exist.


Refer to the pwd command described in Module 111.


The cd command does not display a response unless a problem is encountered. If a problem is reported, it is written to the standard error.


The cd command changes you from your present working directory to a new directory. It is used to move around a file system to perform tasks without having to specify entire paths.

If you are in a directory with the files you need, you can refer to the files as needed. If you are not in the same directory, then you have to specify a path each time you want to access the files. Therefore, cd is a handy tool to move you where you need to be in the file system.


In this activity you use the cd command to move around to various directories on the system. Begin at the shell prompt.

1.  Type cd /usr/spool/uucppublic and press Return. You are now in the uucp spool directory.
2.  Type cd ~/bin and press Return to change directories to your $HOME/bin directory.
3.  Type cd ../db and press Return to change to your $HOME/db directory.
4.  Type cd and press Return to change directories back to your HOME directory.
5.  Turn to Module 111 to continue the learning sequence.

Previous Table of Contents Next