Cd (command)
| Company type | Shell builtin command |
|---|---|
| Industry | Computing / Operating systems |
| Products | Change directory command |
<syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> is a shell instruction used to modify the working directory. This command is present in numerous shells and software that track a working directory. Some systems may utilize different terms for this functionality.
Implementations
Operating systems featuring hierarchical file structures generally provide a change directory command to update the working directory. This command is a standard component of most OS shells, including those based on Unix and Unix-like platforms such as Linux, as well as Microsoft environments like Command Prompt and PowerShell.
Additional operating systems that support this command include OS/2, TRIPOS, and AmigaOS, where the command is implied when specifying an input path. It is also available on ReactOS, DOSBox, and within the UEFI.
- In MS-DOS, the command has been accessible since version 2.
- Both and are supported by DR DOS 6.0.
- On MPE/iX, the equivalent command is .
- For OpenVOS, the command used is .
Unlike many commands executed as standalone programs, changing directories is commonly integrated directly into the shell because it alters the shell’s internal environment. Implementing it externally would necessitate modifying the parent process's state, which is typically prohibited for security reasons. As a result, shells like the Unix family (Bourne shell, tcsh, Bash), Windows’ Command Prompt and PowerShell, and MS-DOS’s COMMAND.COM include it as a built-in feature.
This command usually triggers a system-level call—on Unix-like platforms, this corresponds to the POSIX , whereas Windows relies on its native API.
Outside of shell environments, directory-changing capabilities appear in other applications. For instance, within File Transfer Protocol, the control stream instruction allows remote directory changes; most command-line FTP tools implement this as , often paired with to alter the local working directory. Numerical computing platforms such as MATLAB and GNU Octave also incorporate a cd command for managing directories.
Use
Although usage differs slightly depending on the environment, there are commonalities across Unix-like shells, PowerShell, and Command Prompt.
Directory separators vary: Unix-based shells use forward slashes , Command Prompt employs backslashes , while PowerShell can interpret both. For consistency, forward slashes are used in examples here.
A single dot () denotes the current directory, whereas two dots () signify its parent directory. For instance, in , refers to the present location, while points to . The parent directory notation enables construction of relative paths such as , which navigates upward from to .
Common
Standard capabilities found in any change directory command include:
- — appends a relative path to the current directory; e.g., from , navigates to
- — replaces the current directory with an absolute path; e.g., sets it explicitly to
- — moves up one level in the directory hierarchy; e.g., from , returns to
- — keeps the current location unchanged; useful for refreshing after external directory modifications
- without arguments — returns to the user's home folder; in Command Prompt, it displays the current directory path
Unix shells
Commonly, Unix shells and PowerShell provide support for the following commands:
- — navigates to the user's home directory
cd ~username— accesses the home directory of a specified user- — returns to the last accessed directory; for example, moving from to , then reverting with
Legacy Microsoft shells
In DOS, each drive letter has its own current working directory alongside a designated active drive. The command allows directory changes within the active drive or on other drives. Entering just a drive letter (such as ) switches the current working drive. Using the command with simultaneously updates both the drive and directory.
Contemporary Windows environments replicate this functionality within CMD.EXE to maintain compatibility with older software.
Example
Beginning in the user's home folder (), executing ls followed by yields:
<syntaxhighlight lang="console"> user@wikipedia:~$ ls workreports games encyclopedia text.txt user@wikipedia:~$ cd games user@wikipedia:~/games$ </syntaxhighlight>
An equivalent session in Command Prompt appears as follows:
C:\> dir workreports <DIR> Wed Oct 9th 9:01 games <DIR> Tue Oct 8th 14:32 encyclopedia <DIR> Mon Oct 1st 10:05 text txt 1903 Thu Oct10th 12:43 C:\> cd games C:\games>