Cd (command)
is a shell command that changes the working directory. It is available in many shells and other applications that maintain a working directory. In some contexts, the command can perform actions other than change directory. Some environments provide the change directory feature via a different command name such as.
Implementations
Generally, a computer system that provides access to a hierarchical file system, provides a change directory command to set the working directory. As this applies to most operating system shells, most support a change directory command, including Unix and Unix-like shells, and Microsoft shells including Command Prompt and PowerShell.Other operating systems with shells supporting the command include OS/2, TRIPOS, AmigaOS, ReactOS, DOSBox, and UEFI.
- On MS-DOS, the command is available in version 2 and later
- DR DOS 6.0 includes the command as both and
- On HP MPE/iX the command is
- On OpenVOS, the command is
In a shell, the change directory command is typically implemented via a system call which on Unix and Unix-like systems is typically POSIX and on Windows is in the Windows API.
The command is also provided in many programs other than shells. In the File Transfer Protocol, the control stream command is, but the functionality is available as in most command-line clients and some also provide for changing the local working directory vs. the remote setting. The numerical computing environments MATLAB and GNU Octave include a
change directory command as
cd.Use
Use of the command varies by context, but there are widespread similarities among variants. The examples below, mostly apply to Unix and Unix-like shells, PowerShell and Command Prompt.To separate the directory names of a path, a program imposes command-line syntax such as a delimiting text between names which varies by program. In particular, Unix and Unix-like shells use a forward slash, Command Prompt uses backslash and PowerShell supports either. For simplicity, paths are shown with forward slashes here.
Commonly, a dot is short-hand notation for the working directory and two dots is short-hand for its parent. For example, given working directory, refers to it and refers to. The parent notation is often used to form a relative path that specifies a path that is both up and down the hierarchy. For example: starting with, specifies path.
Common
Features that are commonly found for any change directory command:- For a relative path, the path is appended to the working directory path, moving the context deeper into the directory tree hierarchy; for example, if the working directory is, then changes the working directory to
- For an absolute path, the working directory is replaced with the specified path; for example, sets the working directory to
- Moves the directory tree context up one directory; for example, starting at, changes the working directory to
- Does not change the working directory but is useful to recover after a directory is recreated by another process
- With no arguments, the command changes the working directory to the user's home directory; exception: Command Prompt reports the working directory path
Unix shells
- Changes the working directory to user's home directory
-
cd ~usernameChanges the working directory to the specified user's home directory - Changes the working directory to the previous directory; for example, starting at, executing followed by returns to, which enables toggling between two directories without pushd and popd
Legacy Microsoft shells
Modern versions of Windows simulate this behaviour for backwards compatibility under CMD.EXE.
Example
Starting with working directory set to the user's home, commandls followed by might produce the following:user@wikipedia:~$ ls
workreports games encyclopedia text.txt
user@wikipedia:~$ cd games
user@wikipedia:~/games$
A similar session in Command Prompt might look like:
C:\> dir
workreports
games
encyclopedia
text txt 1903 Thu Oct10th 12:43
C:\> cd games
C:\games>