Cd (command)
158837
216059969
2008-05-30T22:10:00Z
Thijs!bot
1392310
robot Adding: [[it:Cd (Unix)]]
{{Refimprove|date=April 2008}}{{lowercase|title=chdir}}
'''<tt>cd</tt>''', sometimes also available as '''<tt>chdir</tt>''' ('''ch'''ange '''dir'''ectory), is a [[command line interface|command line]] command to change the current [[working directory]] in [[operating system]]s such as [[Unix]], [[DOS]], [[OS/2]] and [[Microsoft Windows|Windows]]. It is also available for use in [[shell script]]s and [[batch file]]s.
'''chdir'''(2) is the [[system call]] which changes the working directory, as defined by [[POSIX]]. '''<code>CHDIR()</code>''' is also a [[Visual Basic]] function which changes the working directory.
==Usage==
A [[directory (file systems)|directory]] is a logical section of a [[file system]] used to hold files. Directories may also contain other directories. The <tt>cd</tt> 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.
[[Image:chdir example.png|center|framed|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.]]
<br clear=all>
If the user's current working directory is the home directory ("~"), then entering the command <code>[[ls]]</code> followed by <code>cd games</code> might produce the following transcript:
me@host:~$ '''ls'''
workreports games encyclopedia text.txt
me@host:~$ '''cd games'''
me@host:~/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) would look like this:
C:\> '''[[dir (command)|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>
Note that <tt>cd</tt> has different effects on different operating systems if given with no arguments. For example, if executed without arguments in DOS, OS/2, or Windows, the current [[working directory]] is printed. If executed without arguments in Unix, then the user is returned to the home directory. The effect of executing the <tt>cd</tt> command within a script or batch file also varies.
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 <tt>cd</tt> command. This is because in Unix, the script is usually executed within a subshell.
==Options (version specific)==
* <tt> -p </tt> Print the final directory stack, just like dirs.
* <tt> -l, '~' or '~name'</tt> in the output is expanded explicitly to home or the pathname of the home directory for user name.
* <tt> -n </tt> Entries are wrapped before they reach the edge of the screen.
* <tt> -v </tt> entries are printed one per line, preceded by their stack positions.
==How it works==
<tt>cd</tt> is frequently included built directly into the [[command line interpreter]]. This is the case in most of the [[Unix shell]]s ([[Bourne shell]], [[tcsh]], [[bash]], etc.), <tt>[[cmd.exe]]</tt> and [[Windows PowerShell]] on Windows and <tt>[[COMMAND.COM]]</tt> on DOS.
Command line shells on Windows usually use the [[Windows API]] to change the current working directory, whereas on Unix systems '''<tt>cd</tt>''' calls the '''<code>chdir()</code>''' [[POSIX]] [[C (programming language)|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 <tt>cd</tt> command inherits the parent process' directory, then the objective of the command cd will never be achieved.
Windows PowerShell, Microsoft's new [[object-oriented programming|object-oriented]] command line shell and [[scripting language]] executes the <tt>cd</tt> 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.
==References==
*[http://technet.microsoft.com/en-us/library/bb490875.aspx Microsoft TechNet Chdir (Cd) article]
*{{man|cu|cd|SUS|change the working directory}}
==See also==
{{Unix commands}}
{{Windows commands}}
[[Category:Unix software]]
[[Category:DOS on IBM PC compatibles]]
[[Category:OS/2]]
[[Category:Windows administration]]
[[cs:Cd (Unix)]]
[[es:Chdir]]
[[fr:Cd (Unix)]]
[[ko:Cd (명령어)]]
[[it:Cd (Unix)]]
[[nl:Cd (Unix)]]
[[ja:Cd (UNIX)]]
[[pl:Cd (Unix)]]
[[pt:Cd (Unix)]]
[[ru:Chdir]]
[[fi:Chdir]]
[[uk:Cd (команда)]]
[[zh:Cd (命令)]]