Windows Administration Codexery

Cd (command)

The cd command shifts the shell's active directory to a different folder. Many shells and programs that track a current directory include this feature. In certain setups, it may also do tasks unrelated to changing folders.

Quick Facts

Developer
  • 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
Genre
Command

Facts from the source article.

Implementations

Most operating system shells that provide access to a hierarchical file system include a change directory command. This includes Unix and Unix-like shells, Linux shells, and Microsoft shells such as Command Prompt and PowerShell. Other operating systems with shells supporting the command include OS/2, TRIPOS, AmigaOS, ReactOS, DOSBox, and UEFI. On MS-DOS, the command is available from version 2 onward.

DR DOS 6.0 provides it as both cd and chdir. On HP MPE/iX the command is chdir, and on OpenVOS it is change_current_dir. Unlike many shell commands implemented as separate applications, change directory is often built into the shell because it affects the shell's own state. If implemented as a separate application, the child process would need to modify state in the parent process, which is typically prevented for safety.

The command is built into most Unix shells, Windows Command Prompt and PowerShell, and MS-DOS COMMAND.COM. In a shell, the command is typically implemented via a system call: on Unix and Unix-like systems this is POSIX chdir, and on Windows it is SetCurrentDirectory in the Windows API. The command also appears in programs other than shells. In FTP, the control stream command is CWD, while most command-line clients use cd, and some provide lcd for changing the local working directory. MATLAB and GNU Octave include a change directory command as cd.

Use

Use of the command varies by context, but widespread similarities exist among variants. To separate directory names in a path, a program imposes a delimiting character that varies: Unix and Unix-like shells use a forward slash, Command Prompt uses a backslash, and PowerShell supports either. A dot is shorthand for the working directory, and two dots for its parent. For a relative path with no leading slash, the path is appended to the working directory path, moving deeper into the hierarchy.

For an absolute path with a leading slash, the working directory is replaced entirely. The two-dot notation moves the context up one directory. With no arguments, the command changes to the user's home directory, except in Command Prompt where it reports the working directory path. Unix-based shells and PowerShell share additional features: cd alone changes to the user's home directory; cd ~username changes to the specified user's home directory; and cd - changes to the previous directory, enabling toggling between two directories without pushd and popd.

DOS maintains separate working directories for each lettered drive and has a concept of a current working drive. The cd command can change the working directory of the working drive or another lettered drive. Typing a drive letter as a command on its own changes the working drive; alternatively, cd with the /d switch may change both 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.

More in Windows Administration

Spotted an error? Know more?

Reader corrections go straight into our review queue. Suggest an edit · How this site is sourced

Comments

Loading…
Open in the interactive codex →