Chdir
Encyclopedia
cd
, sometimes also available as chdir
(change directory), is a command lineCommand-line interface
A command-line interface is a mechanism for interacting with a computer operating system or software by typing commands to perform specific tasks...
command to change the current working directory
Working directory
In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process. When the process refers to a file using a simple file name or relative path , the reference is interpreted relative to the current working directory of...
in operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...
s such as Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
, DOS
DOS
DOS, short for "Disk Operating System", is an acronym for several closely related operating systems that dominated the IBM PC compatible market between 1981 and 1995, or until about 2000 if one includes the partially DOS-based Microsoft Windows versions 95, 98, and Millennium Edition.Related...
, OS/2
OS/2
OS/2 is a computer operating system, initially created by Microsoft and IBM, then later developed by IBM exclusively. The name stands for "Operating System/2," because it was introduced as part of the same generation change release as IBM's "Personal System/2 " line of second-generation personal...
, AmigaOS
AmigaOS
AmigaOS is the default native operating system of the Amiga personal computer. It was developed first by Commodore International, and initially introduced in 1985 with the Amiga 1000...
(where if a bare path is given, cd is implied), Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...
, and Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...
. It is also available for use in shell script
Shell script
A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language...
s and batch file
Batch file
In DOS, OS/2, and Microsoft Windows, batch file is the name given to a type of script file, a text file containing a series of commands to be executed by the command interpreter....
s.
chdir(2) is the system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
which changes the working directory, as defined by POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
.
CHDIR
is also a Visual BasicVisual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
function which changes the working directory.
Usage
A directoryDirectory (file systems)
In computing, a folder, directory, catalog, or drawer, is a virtual container originally derived from an earlier Object-oriented programming concept by the same name within a digital file system, in which groups of computer files and other folders can be kept and organized.A typical file system may...
is a logical section of a file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...
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 directoryRoot directory
In computer file systems, the root directory is the first or top-most directory in a hierarchy. It can be likened to the root of a tree — the starting point where all branches originate.-Metaphor:...
or move to any given directory.
Consider the following subsection of a Unix filesystem, which shows a user's home directory
Home directory
A Home directory is a file system directory on a multi-user operating system containing files for a given user of the system. The specifics of the home directory is defined by the operating system involved; for example, Windows systems between 2000 and 2003 keep home directories in a folder...
(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 lsLsIn computing, ls is a command to list files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification.- History :An ls utility appeared in the original version of AT&T UNIX...
followed by cd games
might produce the following transcript: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) would look like this:
C:\> dir
workreports
games
encyclopedia
text txt 1903 Thu Oct10th 12:43
C:\> cd games
C:\games>
DOS maintains separate working directories for each lettered drive
Drive letter assignment
Drive letter assignment is the process of assigning alphabetical identifiers to physical or logical disk drives or partitions in the root filesystem namespace; this usage is now mostly found in Microsoft operating systems...
, 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
Command Prompt
Command Prompt is the Microsoft-supplied command-line interpreter on OS/2, Windows CE and on Windows NT-based operating systems...
.
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 directoryWorking directory
In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process. When the process refers to a file using a simple file name or relative path , the reference is interpreted relative to the current working directory of...
is displayed. 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 fileBatch file
In DOS, OS/2, and Microsoft Windows, batch file is the name given to a type of script file, a text file containing a series of commands to be executed by the command interpreter....
's use of this command. In Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
, 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 subshellShell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...
.
Options (version specific)
- no attributes Return to the home directory (UNIX) or print the full path of the current directory (DOS and Windows)
- -p Print the final directory stack, just like dirs.
- -l '~' (UNIX only) or '~name' (UNIX only) in the output is expanded explicitly to home or the pathname of the home directory for user name.
- -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 he is located when the command is issued.
Application of some options (unix)
cd by itself or cd ~ will always put you in your home directory.
cd ~username will put you in username's home directory.
cd dir (without a /) will put you in a subdirectory. for example, if you are in /usr, typing cd bin will put you in /usr/bin, while cd /bin puts you in /bin.
cd .. will move you up one directory. So, if you are /usr/bin/tmp, cd .. moves you to /usr/bin, while cd ../.. moves you to /usr (i.e. up two levels). You can use this indirection to access subdirectories too. So, from /usr/bin/tmp, you can use cd ../../local to go to /usr/local.
cd - will switch you to the previous directory. For example, if you are in /usr/bin/tmp, and go to /etc., you can type cd - to go back to /usr/bin/tmp. You can use this to toggle back and forth between two directories.
How it works
cd
is frequently included built directly into the command line interpreter. This is the case in most of the Unix shellUnix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
s (Bourne shell
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...
, tcsh
Tcsh
tcsh is a Unix shell based on and compatible with the C shell . It is essentially the C shell with programmable command line completion, command-line editing, and a few other features.-History:...
, bash, etc.),
cmd.exeCmd.exeCommand Prompt is the Microsoft-supplied command-line interpreter on OS/2, Windows CE and on Windows NT-based operating systems...
and Windows PowerShellWindows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...
on Windows and
COMMAND.COMCOMMAND.COMCOMMAND.COM is the filename of the default operating system shell for DOS operating systems and the default command line interpreter on Windows 95, Windows 98 and Windows Me...
on DOS.Command line shells on Windows usually use the Windows API
Windows API
The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces available in the Microsoft Windows operating systems. It was formerly called the Win32 API; however, the name "Windows API" more accurately reflects its roots in 16-bit Windows and its support on...
to change the current working directory, whereas on Unix systems
cd
calls the chdir
POSIXPOSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
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
Ls
In computing, ls is a command to list files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification.- History :An ls utility appeared in the original version of AT&T UNIX...
. Instead, the shell itself executes this command. This is because, when a new process
Process (computing)
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...
is created, child process
Child process
A child process in computing is a process created by another process .A child process inherits most of its attributes, such as open files, from its parent. In UNIX, a child process is in fact created as a copy of the parent...
inherits the directory in which the parent process
Parent process
In computing, a parent process is a process that has created one or more child processes.- Unix :In the operating system Unix, every process except is created when another process executes the fork system call. The process that invoked fork is the parent process and the newly-created process is...
was created. If the
cd
command inherits the parent process' directory, then the objective of the command cd will never be achieved.Unlike the Windows API
Windows API
The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces available in the Microsoft Windows operating systems. It was formerly called the Win32 API; however, the name "Windows API" more accurately reflects its roots in 16-bit Windows and its support on...
or POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
function, neither the Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
programming language nor the Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...
supports
chdir
directly; a change requestChange request
A change request is a document containing a call for an adjustment of a system; it is of great importance in the change management process. A change request is not raised for a wording change in a letter....
remained open for over a decade while the team responsible for Java considered the alternatives, though by 2008 the request was denied after only limited support was introduced (affecting first
java.lang.Runtime
and later, java.lang.ProcessBuilder
).Windows PowerShell, Microsoft's object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
command line shell and scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
executes the
cd
command (cmdlet) within the shell's process. However, since PowerShell is based on the .NET Framework.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
and has a different architecture than previous shells, all of PowerShell's cmdlets like ls
Ls
In computing, ls is a command to list files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification.- History :An ls utility appeared in the original version of AT&T UNIX...
, rm
RM
RM may refer to:-People and titles:* Radioman, a former rank of the U.S. Navy* Resident Magistrate* Returned Missionary, a phrase used by Mormons to refer to young men and women who have served as full-time missionaries...
etc. run in the shell's process. Of course, this is not true for legacy commands which still run in a separate process.
See also
- Directory structureDirectory structureIn computing, a directory structure is the way an operating system's file system and its files are displayed to the user. Files are typically displayed in a Hierarchical tree structure.-File names and extensions:...
- pushd and popdPushd and popdIn computing, pushd and popd are commands in various Unix, DOS and Microsoft Windows command line interpreters such as Bash, cmd.exe, 4DOS/4NT and Windows PowerShell...
- chrootChrootA chroot on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children. A program that is run in such a modified environment cannot name files outside the designated directory tree. The term "chroot" may refer to the chroot...
- Cd Deluxe