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 |
The cd
command, also known as chdir
(change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.
The command has been implemented in operating systems such as Unix, DOS, IBM OS/2, [1] MetaComCo TRIPOS, [2] AmigaOS [3] (where if a bare path is given, cd is implied), Microsoft Windows, ReactOS, [4] and Linux. On MS-DOS, it is available in versions 2 and later. [5] DR DOS 6.0 also includes an implementation of the cd
and chdir
commands. [6] The command is also available in the open source MS-DOS emulator DOSBox and in the EFI shell. [7] It is named chdir
in HP MPE/iX. [8] The command is analogous to the Stratus OpenVOS change_current_dir
command. [9]
cd
is frequently included built directly into a command-line interpreter. This is the case in most of the Unix shells (Bourne shell, tcsh, bash, etc.), cmd.exe
on Microsoft Windows NT/2000+ and Windows PowerShell on Windows 7+ and COMMAND.COM
on DOS/ Microsoft Windows 3.x-9x/ME.
The system call that effects the command in most operating systems is chdir
that is defined by POSIX.
Command line shells on Windows usually use the Windows API to change the current working directory, whereas on Unix systems cd
calls the chdir()
POSIX C function. This means that when the command is executed, no new process is created to migrate to the other directory as is the case with other commands such as ls. Instead, the shell itself executes this command. This is because, when a new process is created, child process inherits the directory in which the parent process was created. If the cd
command inherits the parent process' directory, then the objective of the command cd will never be achieved.
Windows PowerShell, Microsoft's object-oriented command line shell and scripting language, executes the cd
command (cmdlet) within the shell's process. However, since PowerShell is based on the .NET Framework and has a different architecture than previous shells, all of PowerShell's cmdlets like ls
, rm
etc. run in the shell's process. Of course, this is not true for legacy commands which still run in a separate process.
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.
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.txtuser@wikipedia:~$ cdgames 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. [10]
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.
cd
by itself or cd ~
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; running cd .
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
, typing cd bin
will put them in /usr/bin
, while cd /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
, while cd ../..
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 use cd ../../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 type cd -
to go back to /usr/bin/tmp
. The user can use this to toggle back and forth between two directories without pushd and popd.-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, command cd\subdir
always takes the user to the named subdirectory on the root directory, regardless of where they are located when the command is issued.In the File Transfer Protocol, the respective command is spelled CWD
in the control stream, but is available as cd
in most client command-line programs. Some clients also have the lcd
for changing the working directory locally.
The numerical computing environments MATLAB and GNU Octave include a cd
function with similar functionality. [11] [12] The command also pertains to command-line interpreters of various other application software.
A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be command languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.
In computing, ls
is a command to list computer files and directories in Unix and Unix-like operating systems. It is specified by POSIX and the Single UNIX Specification.
In computing, dir
(directory) is a command in various computer operating systems used for computer file and directory listing. It is one of the basic commands to help navigate the file system. The command is usually implemented as an internal command in the command-line interpreter (shell). On some systems, a more graphical representation of the directory structure can be displayed using the tree
command.
In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with the process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd function, or just current directory. When a process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the working directory of the process. So for example a process with working directory /rabbit-shoes that asks to create the file foo.txt will end up creating the file /rabbit-shoes/foo.txt.
The mkdir
command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md
.
In computing, a symbolic link is a file whose purpose is to point to a file or directory by specifying a path thereto.
In Unix-like and some other operating systems, the pwd
command writes the full pathname of the current working directory to the standard output.
In computing, a command is a directive to a computer program to perform a specific task. It may be issued via a command-line interface or as input to a network service as part of a network protocol, or as an event triggered in a graphical user interface.
An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.
A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter. Paths are used extensively in computer science to represent the directory/file relationships common in modern operating systems and are essential in the construction of Uniform Resource Locators (URLs). Resources can be represented by either absolute or relative paths.
Command Prompt, also known as cmd.exe or cmd, is the default command-line interpreter for the OS/2, eComStation, ArcaOS, Microsoft Windows, and ReactOS operating systems. On Windows CE .NET 4.2, Windows CE 5.0 and Windows Embedded CE 6.0 it is referred to as the Command Processor Shell. Its implementations differ between operating systems, but the behavior and basic set of commands are consistent. cmd.exe is the counterpart of COMMAND.COM in DOS and Windows 9x systems, and analogous to the Unix shells used on Unix-like systems. The initial version of cmd.exe for Windows NT was developed by Therese Stowell. Windows CE 2.11 was the first embedded Windows release to support a console and a Windows CE version of cmd.exe. The ReactOS implementation of cmd.exe is derived from FreeCOM, the FreeDOS command line interpreter.
In Unix-like operating systems, find
is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.
A command shell is a command-line interface to interact with and manipulate a computer's operating system.
File attributes are a type of metadata that describe and may modify how files and/or directories in a filesystem behave. Typical file attributes may, for example, indicate or specify whether a file is visible, modifiable, compressed, or encrypted. The availability of most file attributes depends on support by the underlying filesystem where attribute data must be stored along with other control structures. Each attribute can have one of two states: set and cleared. Attributes are considered distinct from other metadata, such as dates and times, filename extensions or file system permissions. In addition to files, folders, volumes and other file system objects may have attributes.
In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. alias
is available in Unix shells, AmigaDOS, 4DOS/4NT, FreeDOS, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.
In computing, a shebang is the character sequence #!, consisting of the characters number sign and exclamation mark, at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.
A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF
, FOR
, and GOTO
labels. The term "batch" is from batch processing, meaning "non-interactive execution", though a batch file might not process a batch of multiple data.
In computing, pushd
and popd
are a pair of commands which allow users to quickly switch between the current and previous directory when using the command line. When called, they use a directory stack to sequentially save and retrieve directories visited by the user.
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.
A command-line interface (CLI) is a means of interacting with a computer program by inputting lines of text called command-lines. Command-line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user-friendly alternative to the non-interactive interface available with punched cards.