Cd (command)
Template:Short description Template:Lowercase Template:Infobox software <syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> 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 <syntaxhighlight lang="text" class="" style="" inline="1">chdir</syntaxhighlight>.
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 (i.e. Linux) shells, and Microsoft shells including Command Prompt and PowerShell.
Other operating systems with shells supporting the command include OS/2,<ref>Template:Cite web</ref> TRIPOS,<ref>Template:Cite web</ref> AmigaOS<ref>Template:Cite book</ref> (where the command is implied for an input path), ReactOS,<ref>Template:Cite web</ref> DOSBox, and UEFI.<ref name="EFI-Shells-and-Scripting">Template:Cite web</ref>
- On MS-DOS, the command is available in version 2 and later<ref name="RUNNINGMSDOS">Template:Cite book</ref>
- DR DOS 6.0 includes the command as both <syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">chdir</syntaxhighlight><ref name="drdos6userguide">Template:Cite web</ref>
- On HP MPE/iX the command is <syntaxhighlight lang="text" class="" style="" inline="1">chdir</syntaxhighlight><ref>Template:Cite web</ref>
- On OpenVOS, the command is <syntaxhighlight lang="text" class="" style="" inline="1">change_current_dir</syntaxhighlight><ref>Template:Cite web</ref>
Unlike many shell commands that are implemented as separate applications, change directory is often built-in to the shell because it affects the state of the shell whereas other commands modify system state outside the shell. If the command was implemented as a separate application, then the child process would need to modify state in the parent process, but this is often prevented for safety. The command is built-in for most Unix shells (Bourne, tcsh, Bash, etc.), Windows Command Prompt and PowerShell, and MS-DOS COMMAND.COM.
In a shell, the change directory command is typically implemented via a system call which on Unix and Unix-like systems is typically POSIX <syntaxhighlight lang="text" class="" style="" inline="1">chdir()</syntaxhighlight> 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 <syntaxhighlight lang="text" class="" style="" inline="1">CWD</syntaxhighlight>, but the functionality is available as <syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> in most command-line clients and some also provide <syntaxhighlight lang="text" class="" style="" inline="1">lcd</syntaxhighlight> 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.<ref>Template:Cite web</ref><ref>Template:Cite web</ref>
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 Template:Endash which varies by program. In particular, Unix and Unix-like shells use a forward slash <syntaxhighlight lang="text" class="" style="" inline="1">/</syntaxhighlight>, Command Prompt uses backslash <syntaxhighlight lang="text" class="" style="" inline="1">\</syntaxhighlight> 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 <syntaxhighlight lang="text" class="" style="" inline="1">/user/bin/tmp</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">.</syntaxhighlight> refers to it and <syntaxhighlight lang="text" class="" style="" inline="1">..</syntaxhighlight> refers to <syntaxhighlight lang="text" class="" style="" inline="1">/user/bin</syntaxhighlight>. 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 <syntaxhighlight lang="text" class="" style="" inline="1">/usr/bin/tmp</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">cd ../../local</syntaxhighlight> specifies path <syntaxhighlight lang="text" class="" style="" inline="1">/usr/local</syntaxhighlight>.
Common
Features that are commonly found for any change directory command:
- <syntaxhighlight lang="text" class="" style="" inline="1">cd path/to/dir</syntaxhighlight> For a relative path (no leading slash), 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 <syntaxhighlight lang="text" class="" style="" inline="1">/usr</syntaxhighlight>, then <syntaxhighlight lang="text" class="" style="" inline="1">cd bin</syntaxhighlight> changes the working directory to <syntaxhighlight lang="text" class="" style="" inline="1">/usr/bin</syntaxhighlight>
- <syntaxhighlight lang="text" class="" style="" inline="1">cd /path/to/dir</syntaxhighlight> For an absolute path (leading slash), the working directory is replaced with the specified path; for example, <syntaxhighlight lang="text" class="" style="" inline="1">cd /bin</syntaxhighlight> sets the working directory to <syntaxhighlight lang="text" class="" style="" inline="1">/bin</syntaxhighlight>
- <syntaxhighlight lang="text" class="" style="" inline="1">cd ..</syntaxhighlight> Moves the directory tree context up one directory; for example, starting at <syntaxhighlight lang="text" class="" style="" inline="1">/usr/bin/tmp</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">cd ..</syntaxhighlight> changes the working directory to <syntaxhighlight lang="text" class="" style="" inline="1">/usr/bin</syntaxhighlight>
- <syntaxhighlight lang="text" class="" style="" inline="1">cd .</syntaxhighlight> Does not change the working directory but is useful to recover after a directory is recreated by another process
- <syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> 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
Unix-based shells and PowerShell generally share these features:
- <syntaxhighlight lang="text" class="" style="" inline="1">cd ~</syntaxhighlight> Changes the working directory to user's home directory
cd ~usernameChanges the working directory to the specified user's home directory- <syntaxhighlight lang="text" class="" style="" inline="1">cd -</syntaxhighlight> Changes the working directory to the previous directory; for example, starting at <syntaxhighlight lang="text" class="" style="" inline="1">/usr/bin/tmp</syntaxhighlight>, then <syntaxhighlight lang="text" class="" style="" inline="1">cd /etc</syntaxhighlight>, and then <syntaxhighlight lang="text" class="" style="" inline="1">cd -</syntaxhighlight> returns to <syntaxhighlight lang="text" class="" style="" inline="1">/usr/bin/tmp</syntaxhighlight>; this supports toggling between two directories without pushd and popd
Legacy Microsoft shells
DOS maintains separate working directories for each lettered drive, and also has the concept of a current working drive. The <syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> command can be used to change the working directory of the working drive or another lettered drive. Typing the drive letter as a command on its own changes the working drive, e.g. <syntaxhighlight lang="text" class="" style="" inline="1">C:</syntaxhighlight>; alternatively, <syntaxhighlight lang="text" class="" style="" inline="1">cd</syntaxhighlight> with the <syntaxhighlight lang="text" class="" style="" inline="1">/d</syntaxhighlight> switch may be used to change the working drive and that drive's working directory in one step. Modern versions of Windows simulate this behaviour for backwards compatibility under CMD.EXE.<ref>Template:Cite web</ref>
Example
Starting with working directory set to the user's home (<syntaxhighlight lang="text" class="" style="" inline="1">~</syntaxhighlight>), command ls followed by <syntaxhighlight lang="text" class="" style="" inline="1">cd games</syntaxhighlight> might produce the following:
<syntaxhighlight lang="console"> user@wikipedia:~$ ls workreports games encyclopedia text.txt user@wikipedia:~$ cd games user@wikipedia:~/games$ </syntaxhighlight>
A similar session in Command Prompt might look like:
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>
See also
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
References
Further reading
External links
Template:Wikibooks Template:Wikibooks
Template:Unix commands Template:Windows commands Template:Portalbar