Cd (command)

Command to change the working directory

cd / chdir

The `cd` command illustration (in a shell session in Debian GNU/Linux)

Developer(s)| AT&T Bell Laboratories, MetaComCo, Microsoft, IBM, DR, Novell, HP, JP Software, ReactOS Contributors
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
Type| Command

`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 `chdir`.

## 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,[1] TRIPOS,[2] AmigaOS[3] (where the command is implied for an input path), ReactOS,[4] DOSBox, and UEFI.[5]

* On MS-DOS, the command is available in version 2 and later[6]
* DR DOS 6.0 includes the command as both `cd` and `chdir`[7]
* On HP MPE/iX the command is `chdir`[8]
* On OpenVOS, the command is `change_current_dir`[9]

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 `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 `CWD`, but the functionality is available as `cd` in most command-line clients and some also provide `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`.[10][11]

## 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 – 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 `/user/bin/tmp`, `.` refers to it and `..` refers to `/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 `/usr/bin/tmp`, `cd ../../local` specifies path `/usr/local`.

### Common

[edit]

Features that are commonly found for any change directory command:

* `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 `/usr`, then `cd bin` changes the working directory to `/usr/bin`
* `cd /path/to/dir` For an absolute path (leading slash), the working directory is replaced with the specified path; for example, `cd /bin` sets the working directory to `/bin`
* `cd ..` Moves the directory tree context up one directory; for example, starting at `/usr/bin/tmp`, `cd ..` changes the working directory to `/usr/bin`
* `cd .` Does not change the working directory but is useful to recover after a directory is recreated by another process
* `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

[edit]

Unix-based shells and PowerShell generally share these features:

* `cd ~` Changes the working directory to user's home directory
* `cd ~username` Changes the working directory to the specified user's home directory
* `cd -` Changes the working directory to the previous directory; for example, starting at `/usr/bin/tmp`, then `cd /etc`, and then `cd -` returns to `/usr/bin/tmp`; 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 `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. `C:`; alternatively, `cd` with the `/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.[12]

## Example

[edit]

Section of a Unix filesystem showing a user's home directory (represented as `~`) with a file and three subdirectories.

Starting with working directory set to the user's home (`~`), command `ls` followed by `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        <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]

* Directory structure – Hierarchical structure of a computer's file system
* chroot – Shell command and system call that changes the apparent root directory
* List of command-line interpreters
* pushd and popd – Shell commands to save and restore the working directory
* pwd – Shell command that reports the working directory

## References

[edit]

1. ^ "JaTomes Help – OS/2 Commands". www.jatomes.com. Archived from the original on 2019-04-14. Retrieved 2019-08-11.
2. ^ "Introduction to Tripos" (PDF). Retrieved 2020-05-01.
3. ^ Rügheimer, Hannes; Spanik, Christian (May 1, 1988). AmigaDOS quick reference. Grand Rapids, Mi : Abacus. ISBN 9781557550491 – via Internet Archive.
4. ^ "Reactos/reactos". GitHub. 3 January 2022.
5. ^ "EFI Shells and Scripting". Intel. Retrieved 2013-09-25.
6. ^ Wolverton, Van (2003). Running MS-DOS Version 6.22 (20th Anniversary Edition), 6th Revised edition. Microsoft Press. ISBN 0-7356-1812-7.
7. ^ "DR DOS 6.0 User Guide Optimisation and Configuration Tips" (PDF). Archived from the original (PDF) on 2019-09-30. Retrieved 2019-08-13.
8. ^ "MPE/iX Command Reference Manual" (PDF). Archived from the original (PDF) on 2018-10-21. Retrieved 2018-10-21.
9. ^ "OpenVOS Commands Reference Manual" (PDF). stratadoc.stratus.com. Retrieved 2020-09-12.
10. ^ "Change current folder – MATLAB cd". www.mathworks.com.
11. ^ "Function Reference: cd". octave.sourceforge.io.
12. ^ "October 11, 2010". The Old New Thing.

## Further reading

[edit]

* Wolverton, Van (1990). MS-DOS Commands: Microsoft Quick Reference, 4th Revised edition. Microsoft Press. ISBN 978-1-55615-289-4.
* John Paul Mueller (2007). Windows Administration at the Command Line for Windows Vista, Windows 2003, Windows XP, and Windows 2000. John Wiley & Sons. ISBN 978-0-470-16579-9.
* Barrett, Daniel J. (2012). Macintosh Terminal Pocket Guide: Take Command of Your Mac. O'Reilly. ISBN 978-1-4493-2898-6.

## External links

[edit]

Wikibooks has a book on the topic of: Guide to Windows Commands

The Wikibook Guide to Unix has a page on the topic of: Commands

* Windows XP > Command-line reference A-Z > Chdir (Cd) from Microsoft TechNet
* `cd`: change the working directory – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group

* v
* t
* e

Unix command-line interface programs and shell builtins

File system|

* cat
* chattr
* chmod
* chown
* chgrp
* cksum
* cmp
* cp
* dd
* du
* df
* file
* fuser
* ln
* ls
* mkdir
* mv
* pax
* pwd
* rm
* rmdir
* split
* tee
* touch
* type
* umask

Processes|

* at
* bg
* crontab
* fg
* kill
* nice
* ps
* time

User environment|

* env
* exit
* logname
* mesg
* talk
* tput
* uname
* who
* write

Text processing|

* awk
* basename
* comm
* csplit
* cut
* diff
* dirname
* ed
* ex
* fold
* head
* iconv
* join
* m4
* more
* nl
* paste
* patch
* printf
* read
* sed
* sort
* strings
* tail
* tr
* troff
* uniq
* vi
* wc
* xargs

Shell builtins|

* alias
* cd
* echo
* test
* unset
* wait

Searching|

* find
* grep

Documentation|

* man

Software development|

* ar
* ctags
* lex
* make
* nm
* strip
* yacc

Miscellaneous|

* bc
* cal
* expr
* lp
* od
* sleep
* true and false

* Categories
* Standard Unix programs
* Unix SUS2008 utilities
* List

* v
* t
* e

Windows command-line programs and shell builtins

Ecosystem|

| Interpreters|

* COMMAND.COM
* Command Prompt
* PowerShell
* Recovery Console

|

Terminals|

* Windows Console
* Windows Terminal

File system navigation|

* cd (chdir)
* dir
* popd
* pushd
* tree

File management|

* attrib
* cacls
* cipher
* compact
* copy
* del (erase)
* deltree
* icacls
* mkdir (md)
* mklink
* move
* openfiles
* recover
* ren (rename)
* replace
* rmdir (rd)
* robocopy
* takeown
* xcopy

Archiving|

* expand
* extrac32
* extract
* makecab
* pax
* tar

Disk management|

* chkdsk
* convert
* defrag
* diskcomp
* diskcopy
* diskpart
* diskraid
* diskshadow
* drvspace
* fdisk
* format
* fsutil
* label
* manage-bde
* MSCDEX
* refsutil
* subst
* scandisk
* sys
* vol
* vssadmin

Processes|

* at
* exit
* kill
* runas
* sc
* schtasks
* shutdown
* start
* taskkill
* tasklist

Registry|

* assoc
* ftype
* reg
* regini
* regsvr32

User environment|

* chcp
* cmdkey
* date
* graftabl
* mode
* path
* set
* setver
* setx
* time
* title
* ver
* where
* whoami

File contents|

* comp
* edit
* edlin
* fc
* find
* findstr
* print
* type

Scripting|

* choice
* clip
* cscript
* doskey
* echo
* for
* forfiles
* goto
* if
* more
* pause
* prompt
* rem
* timeout

Networking|

* arp
* bitsadmin
* curl
* getmac
* hostname
* ipconfig
* nbtstat
* net
* netsh
* netstat
* nslookup
* PathPing
* ping
* rpcping
* route
* scp
* setspn
* sftp
* ssh
* ssh-add
* ssh-agent
* ssh-keygen
* ssh-keyscan
* tracert
* winrm
* winrs

Maintenance and care|

* auditpol
* dism
* dispdiag
* driverquery
* eventcreate
* eventtriggers
* gpresult
* gpupdate
* logman
* mofcomp
* msiexec
* ntbackup
* pentnt
* pnpunattend
* pnputil
* REAgentC
* relog
* sfc
* sxstrace
* systeminfo
* tpmtool
* tpmvscmgr
* tracerpt
* typeperf
* w32tm
* WBAdmin
* wecutil
* wevtutil
* winmgmt
* winsat
* wmic

Boot management|

* bcdedit
* bootcfg
* bootsect
* fixboot
* fixmbr

Software development|

* break
* debug
* exe2bin
* QBasic
* wsl

Miscellaneous|

* cls
* dpath
* help

* List of DOS commands
* Environment variables
* Windows Support Tools

Portal:

* Free and open-source software

*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template