cd (command)

{{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,{{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}} TRIPOS,{{cite web|url=https://www.pagetable.com/docs/amigados_tripos/tripos_manuals.pdf |title=Introduction to Tripos |access-date=2020-05-01}} AmigaOS{{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}} (where the command is implied for an input path), ReactOS,{{cite web|url=https://github.com/reactos/reactos/blob/master/base/shell/cmd/internal.c|title=Reactos/reactos|website=GitHub|date=3 January 2022}} DOSBox, and UEFI.{{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

}}

  • On MS-DOS, the command is available in version 2 and later{{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}}
  • DR DOS 6.0 includes the command as both {{code|cd}} and {{code|chdir}}{{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 }}
  • On HP MPE/iX the command is {{code|chdir}}{{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}}
  • On OpenVOS, the command is {{code|change_current_dir}}{{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}}

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 local working directory vs. the remote setting. The numerical computing environments MATLAB and GNU Octave include a

change directory command as cd.{{cite web|url=https://www.mathworks.com/help/matlab/ref/cd.html|title=Change current folder – MATLAB cd|website=www.mathworks.com}}{{cite web|url=https://octave.sourceforge.io/octave/function/cd.html|title=Function Reference: cd|website=octave.sourceforge.io}}

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
  • cd ~username 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}}, then {{code|cd /etc}}, and then {{code|cd -}} returns to {{code|/usr/bin/tmp}}; 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 {{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.{{Cite web|url=https://devblogs.microsoft.com/oldnewthing/2010/10/11|title=October 11, 2010|website=The Old New Thing}}

Example

File:chdir example.png (represented as {{code|~}}) with a file and three subdirectories.]]

Starting with working directory set to the user's home ({{code|~}}), command ls followed by {{code|cd games}} 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

Wed Oct 9th 9:01

games

Tue Oct 8th 14:32

encyclopedia

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}}