Cd (command)
Template:Short description Template:Lowercase Template:Infobox software Template:Code 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 Template:Code.
Implementations
[edit]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 Template:Code and Template:Code<ref name="drdos6userguide">Template:Cite web</ref>
- On HP MPE/iX the command is Template:Code<ref>Template:Cite web</ref>
- On OpenVOS, the command is Template:Code<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 Template:Code 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 Template:Code, but the functionality is available as Template:Code in most command-line clients and some also provide Template:Code 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
[edit]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 Template:Code, Command Prompt uses backslash Template:Code 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 Template:Code, Template:Code refers to it and Template:Code refers to Template:Code. 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 Template:Code, Template:Code specifies path Template:Code.
Common
[edit]Features that are commonly found for any change directory command:
- Template:Code 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 Template:Code, then Template:Code changes the working directory to Template:Code
- Template:Code For an absolute path (leading slash), the working directory is replaced with the specified path; for example, Template:Code sets the working directory to Template:Code
- Template:Code Moves the directory tree context up one directory; for example, starting at Template:Code, Template:Code changes the working directory to Template:Code
- Template:Code Does not change the working directory but is useful to recover after a directory is recreated by another process
- Template:Code 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
[edit]Unix-based shells and PowerShell generally share these features:
- Template:Code Changes the working directory to user's home directory
cd ~username
Changes the working directory to the specified user's home directory- Template:Code Changes the working directory to the previous directory; for example, starting at Template:Code, then Template:Code, and then Template:Code returns to Template:Code; this supports toggling between two directories without pushd and popd
Legacy Microsoft shells
[edit]DOS maintains separate working directories for each lettered drive, and also has the concept of a current working drive. The Template:Code 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. Template:Code; alternatively, Template:Code with the Template:Code 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
[edit]Starting with working directory set to the user's home (Template:Code), command ls
followed by Template:Code 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
[edit]- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
References
[edit]Further reading
[edit]External links
[edit]Template:Wikibooks Template:Wikibooks
Template:Unix commands Template:Windows commands Template:Portalbar