cd (command)
![]() | |
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 command that changes the working directory – available in many operating system shells and 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
andchdir
[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]
Usage
[edit]A directory is a logical section of a file system used to hold files. Directories may also contain other directories. The cd
command can be used to change into a subdirectory, move back into the parent directory, move all the way back to the root directory or move to any given directory.
Consider the following subsection of a Unix filesystem, which shows a user's home directory (represented as ~
) with a file, text.txt
, and three subdirectories.

~
). From there, the tree can spread into more subdirectories and/or files.If the user's current working directory is the home directory (~
), then entering the command ls
followed by cd games
might produce the following transcript:
user@wikipedia:~$ ls
workreports games encyclopedia text.txt
user@wikipedia:~$ cd games
user@wikipedia:~/games$
The user is now in the "games" directory.
A similar session in DOS (though the concept of a "home directory" may not apply, depending on the specific version[vague]) would look like this:
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>
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]
Note that executing cd
from the command line with no arguments has different effects in different operating systems. For example, if cd
is executed without arguments in DOS, OS/2, or Windows, the current working directory is displayed (equivalent to Unix pwd
). If cd
is executed without arguments in Unix, the user is returned to the home directory.
Executing the cd
command within a script or batch file also has different effects in different operating systems. In DOS, the caller's current directory can be directly altered by the batch file's use of this command. In Unix, the caller's current directory is not altered by the script's invocation of the cd
command. This is because in Unix, the script is usually executed within a subshell.
Options
[edit]Unix, Unix-like
[edit]cd
by itself orcd ~
will always put the user in their home directory.cd .
will leave the user in the same directory they are currently in (i.e. the current directory won't change). This can be useful if the user's shell's internal code can't deal with the directory they are in being recreated; runningcd .
will place their shell in the recreated directory.cd ~username
will put the user in the username's home directory.cd dir
(without a/
) will put the user in a subdirectory; for example, if they are in/usr
, typingcd bin
will put them in/usr/bin
, whilecd /bin
puts them in/bin
.cd ..
will move the user up one directory. So, if they are/usr/bin/tmp
,cd ..
moves them to/usr/bin
, whilecd ../..
moves them to/usr
(i.e. up two levels). The user can use this indirection to access subdirectories too. So, from/usr/bin/tmp
, they can usecd ../../local
to go to/usr/local
cd -
will switch the user to the previous directory. For example, if they are in/usr/bin/tmp
, and go to/etc
, they can typecd -
to go back to/usr/bin/tmp
. The user can use this to toggle back and forth between two directories without pushd and popd.
DOS, OS/2, Windows, ReactOS
[edit]- no attributes print the full path of the current directory.
-p
Print the final directory stack, just like dirs.-n
Entries are wrapped before they reach the edge of the screen.-v
entries are printed one per line, preceded by their stack positions.cd\
(DOS and Windows only) returns to the root dir. Consequently, commandcd\subdir
always takes the user to the named subdirectory on the root directory, regardless of where they are located when the command is issued.
See also
[edit]- Directory structure – Hierarchical structure of a computer's file system
- pushd and popd – Commands
- chroot – Operation that changes the apparent root directory in Unix-like systems
- List of command-line interpreters
References
[edit]- ^ "JaTomes Help – OS/2 Commands". www.jatomes.com. Archived from the original on 2019-04-14. Retrieved 2019-08-11.
- ^ "Introduction to Tripos" (PDF). Retrieved 2020-05-01.
- ^ Rügheimer, Hannes; Spanik, Christian (May 1, 1988). AmigaDOS quick reference. Grand Rapids, Mi : Abacus. ISBN 9781557550491 – via Internet Archive.
- ^ "Reactos/reactos". GitHub. 3 January 2022.
- ^ "EFI Shells and Scripting". Intel. Retrieved 2013-09-25.
- ^ Wolverton, Van (2003). Running MS-DOS Version 6.22 (20th Anniversary Edition), 6th Revised edition. Microsoft Press. ISBN 0-7356-1812-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.
- ^ "MPE/iX Command Reference Manual" (PDF). Archived from the original (PDF) on 2018-10-21. Retrieved 2018-10-21.
- ^ "OpenVOS Commands Reference Manual" (PDF). stratadoc.stratus.com. Retrieved 2020-09-12.
- ^ "Change current folder – MATLAB cd". www.mathworks.com.
- ^ "Function Reference: cd". octave.sourceforge.io.
- ^ "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]- Windows XP > Command-line reference A-Z > Chdir (Cd) from Microsoft TechNet
- The Single UNIX Specification, Version 4 from The Open Group : change the working directory – Shell and Utilities Reference,