Cd (command)

Last updated
cd / chdir
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.

Contents

Implementations

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.

Usage

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.

A user's view of the file system in Unix-like systems begins with the home directory (often abbreviated to ~). From there, the tree can spread into more subdirectories and/or files. Chdir example.png
A user's view of the file system in Unix-like systems begins with the home directory (often abbreviated to ~). 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.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.

Options

Unix, Unix-like

DOS, OS/2, Windows, ReactOS

Interpreters other than an operating systems shell

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.

See also

Related Research Articles

<span class="mw-page-title-main">Shell script</span> Script written for the shell, or command line interpreter, of an operating system

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 scripting 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.

ls Command to list files and directories in Unix and Unix-like operating systems

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.

dir (command) Directory information command on various operating systems

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 each process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd function, or just current directory. When the 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.

mkdir Directory creation command on various operating systems

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.

pwd Directory information command on various operating systems

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, such as a shell, or as input to a network service as part of a network protocol, or as an event in a graphical user interface triggered by the user selecting an option in a menu.

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.

cmd.exe Command prompt program

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 and some other 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.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

File attributes are a type of meta-data 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.

alias (command) Command in various command line interpreters

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, 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 commands used to work with the command line directory stack. They are available on command-line interpreters such as 4DOS, Bash, C shell, tcsh, Hamilton C shell, KornShell, cmd.exe, and PowerShell for operating systems such as Windows and Unix-like systems.

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.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

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 a user-friendly alternative to punched cards.

References

  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. Wolverton, Van (2003). Running MS-DOS Version 6.22 (20th Anniversary Edition), 6th Revised edition. Microsoft Press. ISBN   0-7356-1812-7.
  6. "DR DOS 6.0 User Guide Optimisation and Configuration Tips" (PDF). Archived from the original (PDF) on 2019-09-30. Retrieved 2019-08-13.
  7. "EFI Shells and Scripting". Intel . Retrieved 2013-09-25.
  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. "October 11, 2010". The Old New Thing.
  11. "Change current folder – MATLAB cd". www.mathworks.com.
  12. "Function Reference: cd". octave.sourceforge.io.

Further reading