cd Command
Purpose
Changes the current directory.
Syntax
cd [directory]
or
cd [directorya directoryb]
Description
The cd command sets the current working directory of a process. The user must have execute (search) permission in the specified directory.
If a directory parameter is not specified, the cd command sets the current working directory to the login directory ($HOME in the ksh and bsh environments, or $home in the csh environment). If the specified directory name is a full path name, it becomes the current working directory. A full path name begins with a / (slash) indicating root directory, a . (dot) indicating current directory, or a .. (dot-dot) indicating parent directory. If the directory name is not a full path name, the cd command searches for it relative to one of the paths specified by the $CDPATH shell variable (or $cdpath csh variable). If the cd command is unsuccessful in searching the components, it throws the failure message of the last component it searched. This variable has the same syntax as, and similar semantics to, the $PATH shell variable (or $path csh variable).
Exit Status
This command returns the following exit values:
Item | Description |
---|---|
0 | Successful completion. |
>0 | An error occurred. |
Examples
- To change the current working directory to the login (home) directory, type:
cd
- To change to an arbitrary directory, type:
This changes the current directory to /usr/include.cd /usr/include
- To go down one level of the directory tree, type:
If the current directory is /usr/include and it contains a subdirectory named sys, then /usr/include/sys becomes the current directory.cd sys
- To go up one level of the directory tree, type:
The special file name, .. (dot-dot), refers to the directory immediately above the current directory.cd ..
- Specifying two directory parameters substitutes the string directoryb for the string
directorya in the current working directory, then makes the new path the current directory.
For example, if the current working directory is
the command/home/directorya/sub1/sub2/sub3/sub4
will set the current working directory tocd directorya directoryb
if that directory exists. Additionally, if the current working directory is:/home/directoryb/sub1/sub2/sub3/sub4
the commandhome/directorya/sub1/sub2/sub3/sub4
will set the current working directory tocd directorya directoryb/test
if that directory exists. Likewise, if the current working directory ishome/directoryb/test/sub1/sub2/sub3/sub4
the command/home/directoryb/test/sub1/sub2/sub3/sub4
will set the current working directory tocd directoryb/test directorya
if that directory exists.home/directorya/sub1/sub2/sub3/sub4
Subdirectories must all have the same name.