{{Short description|Command to change the working directory}} {{lowercase}} {{Infobox software | name = cd / chdir | logo = | screenshot = Cd command.png | screenshot size = | caption = The {{code|cd}} command illustration (in a shell session in Debian GNU/Linux) | developer = AT&T Bell Laboratories, MetaComCo, Microsoft, IBM, DR, Novell, HP, JP Software, ReactOS Contributors | released = | latest release version = | latest release date = | operating system = Unix, Unix-like, V, DOS, MSX-DOS, FlexOS, OS/2, TRIPOS, Windows, MPE/iX, Plan 9, Inferno, ReactOS, KolibriOS, SymbOS | platform = Cross-platform | genre = Command | license = | website = }} '''{{code|cd}}''' 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 {{code|chdir}}.
== 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>{{cite web|url=http://www.jatomes.com/Help/Os2Cmd.php#CD|title=JaTomes Help – OS/2 Commands|website=www.jatomes.com|access-date=2019-08-11|archive-date=2019-04-14|archive-url=https://web.archive.org/web/20190414130029/http://www.jatomes.com/Help/Os2Cmd.php#CD|url-status=dead}}</ref> TRIPOS,<ref>{{cite web |url=https://www.pagetable.com/docs/amigados_tripos/tripos_manuals.pdf |title=Introduction to Tripos |access-date=2020-05-01 |archive-date=2020-10-21 |archive-url=https://web.archive.org/web/20201021043218/https://www.pagetable.com/docs/amigados_tripos/tripos_manuals.pdf |url-status=live }}</ref> AmigaOS<ref>{{cite book|url=http://archive.org/details/1988-rugheimer-spanik-amigados-quick-reference|title=AmigaDOS quick reference|first1=Hannes|last1=Rügheimer|first2=Christian|last2=Spanik|date=May 1, 1988|publisher=Grand Rapids, Mi : Abacus|isbn=9781557550491|via=Internet Archive}}</ref> (where the command is implied for an input path), ReactOS,<ref>{{cite web|url=https://github.com/reactos/reactos/blob/master/base/shell/cmd/internal.c|title=Reactos/reactos|website=GitHub|date=3 January 2022|access-date=11 August 2019|archive-date=31 March 2022|archive-url=https://web.archive.org/web/20220331204402/https://github.com/reactos/reactos/blob/master/base/shell/cmd/internal.c|url-status=live}}</ref> DOSBox, and UEFI.<ref name="EFI-Shells-and-Scripting">{{cite web | url = http://software.intel.com/en-us/articles/efi-shells-and-scripting/ | title = EFI Shells and Scripting | publisher = Intel | access-date = 2013-09-25 | archive-date = 2013-09-27 | archive-url = https://web.archive.org/web/20130927203229/http://software.intel.com/en-us/articles/efi-shells-and-scripting/ | url-status = live }}</ref>
* On MS-DOS, the command is available in version 2 and later<ref name="RUNNINGMSDOS">{{Cite book|author-last=Wolverton|author-first=Van|title=Running MS-DOS Version 6.22 (20th Anniversary Edition), 6th Revised edition|date=2003|publisher=Microsoft Press|isbn=0-7356-1812-7}}</ref> * DR DOS 6.0 includes the command as both {{code|cd}} and {{code|chdir}}<ref name="drdos6userguide">{{Cite web |url=https://www.4corn.co.uk/archive/docs/DR%20DOS%206.0%20User%20Guide-opt.pdf |title=DR DOS 6.0 User Guide Optimisation and Configuration Tips |access-date=2019-08-13 |archive-url=https://web.archive.org/web/20190930135943/http://www.4corn.co.uk/archive/docs/DR%20DOS%206.0%20User%20Guide-opt.pdf |archive-date=2019-09-30 |url-status=dead }}</ref> * On HP MPE/iX the command is {{code|chdir}}<ref>{{Cite web|url=http://www.teamnaconsulting.com/compresources/pdfs/c01687363.pdf|title=MPE/iX Command Reference Manual|access-date=2018-10-21|archive-date=2018-10-21|archive-url=https://web.archive.org/web/20181021232213/http://www.teamnaconsulting.com/compresources/pdfs/c01687363.pdf|url-status=dead}}</ref> * On OpenVOS, the command is {{code|change_current_dir}}<ref>{{cite web |url=http://stratadoc.stratus.com/vos/19.1.0/r098-19/wwhelp/wwhimpl/common/html/r098-19.pdf |title=OpenVOS Commands Reference Manual |website=stratadoc.stratus.com |access-date=2020-09-12 |archive-date=2019-09-22 |archive-url=https://web.archive.org/web/20190922035829/https://stratadoc.stratus.com/vos/19.1.0/r098-19/wwhelp/wwhimpl/common/html/r098-19.pdf |url-status=live }}</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 {{code|chdir()}} 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 {{code|CWD}}, but the functionality is available as {{code|cd}} in most command-line clients and some also provide {{code|lcd}} for changing the <u>l</u>ocal working directory vs. the remote setting. The numerical computing environments MATLAB and GNU Octave include a change directory command as <code>cd</code>.<ref>{{cite web|url=https://www.mathworks.com/help/matlab/ref/cd.html|title=Change current folder – MATLAB cd|website=www.mathworks.com}}</ref><ref>{{cite web|url=https://octave.sourceforge.io/octave/function/cd.html|title=Function Reference: cd|website=octave.sourceforge.io|access-date=2019-04-06|archive-date=2019-04-06|archive-url=https://web.archive.org/web/20190406164600/https://octave.sourceforge.io/octave/function/cd.html|url-status=live}}</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 {{endash}} which varies by program. In particular, Unix and Unix-like shells use a forward slash {{code|/}}, Command Prompt uses backslash {{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 {{code|/user/bin/tmp}}, {{code|.}} refers to it and {{code|..}} refers to {{code|/user/bin}}. 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 {{code|/usr/bin/tmp}}, {{code|cd ../../local}} specifies path {{code|/usr/local}}.
=== Common === Features that are commonly found for any change directory command: * {{code|cd path/to/dir}} 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 {{code|/usr}}, then {{code|cd bin}} changes the working directory to {{code|/usr/bin}} * {{code|cd /path/to/dir}} For an absolute path (leading slash), the working directory is replaced with the specified path; for example, {{code|cd /bin}} sets the working directory to {{code|/bin}} * {{code|cd ..}} Moves the directory tree context up one directory; for example, starting at {{code|/usr/bin/tmp}}, {{code|cd ..}} changes the working directory to {{code|/usr/bin}} * {{code|cd .}} Does not change the working directory but is useful to recover after a directory is recreated by another process * {{code|cd}} 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: * {{code|cd ~}} Changes the working directory to user's home directory * <code>cd ~''username''</code> Changes the working directory to the specified user's home directory * {{code|cd -}} Changes the working directory to the previous directory; for example, starting at {{code|/usr/bin/tmp}}, executing {{code|cd /etc}} followed by {{code|cd -}} returns to {{code|/usr/bin/tmp}}, which enables 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 {{code|cd}} 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. {{code|C:}}; alternatively, {{code|cd}} with the {{code|/d}} 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>{{Cite web|url=https://devblogs.microsoft.com/oldnewthing/2010/10/11|title=October 11, 2010|website=The Old New Thing|access-date=September 12, 2020|archive-date=October 29, 2020|archive-url=https://web.archive.org/web/20201029015430/https://devblogs.microsoft.com/oldnewthing/2010/10/11|url-status=live}}</ref>
== Example == [[File:chdir example.png|center|framed|Section of a Unix filesystem showing a user's home directory (represented as {{code|~}}) with a file and three subdirectories.]]
Starting with working directory set to the user's home ({{code|~}}), command <code>ls</code> followed by {{code|cd games}} 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 == * {{Annotated link|Directory structure}} * {{Annotated link|chroot}} * {{Annotated link|List of command-line interpreters}} * {{Annotated link|pushd and popd}} * {{Annotated link|pwd}}
== References == {{Reflist}}
== Further reading == *{{Cite book|author-last=Wolverton|author-first=Van|title=MS-DOS Commands: Microsoft Quick Reference, 4th Revised edition|date=1990|publisher=Microsoft Press|isbn=978-1-55615-289-4}} *{{Cite book|author=John Paul Mueller|year=2007|title=Windows Administration at the Command Line for Windows Vista, Windows 2003, Windows XP, and Windows 2000|publisher=John Wiley & Sons|isbn=978-0-470-16579-9}} *{{Cite book|first=Daniel J.|last=Barrett|year=2012|title=Macintosh Terminal Pocket Guide: Take Command of Your Mac|publisher=O'Reilly|isbn=978-1-4493-2898-6}}
== External links == {{Wikibooks|Guide to Windows Commands}} {{Wikibooks|Guide to Unix|Commands}} * [https://technet.microsoft.com/en-us/library/bb490875.aspx Windows XP > Command-line reference A-Z > Chdir (Cd)] from Microsoft TechNet * {{man|cu|cd|SUS|change the working directory}}
{{Unix commands}} {{Windows commands}} {{Portalbar|Free and open-source software}}
Category:Internal DOS commands Category:File system directories Category:Inferno (operating system) commands Category:IBM i Qshell commands Category:MSX-DOS commands Category:OS/2 commands Category:ReactOS commands Category:Windows administration Category:Standard Unix programs Category:Unix SUS2008 utilities