Jump to content

Cd (command): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
FrescoBot (talk | contribs)
m Bot: link specificity and minor changes
..dtto (desc.)
 
(29 intermediate revisions by 19 users not shown)
Line 1: Line 1:
{{Short description|Computer command in various operating systems}}
{{lowercase}}
{{lowercase}}
{{Infobox software
{{Infobox software
| name = cd / chdir
| name = cd / chdir
| logo =
| logo =
| screenshot = Plan 9 from Bell Labs (process management).png
| screenshot = Cd command.png
| screenshot size =
| screenshot size =
| caption = The {{code|cd}} command in an [[rc]] session running on [[Plan 9 from Bell Labs]]
| caption = The {{code|cd}} command illustration (in a shell session in [[Debian GNU]]/[[Linux]])
| developer = [[AT&T Bell Laboratories]], [[MetaComCo]], [[Microsoft]], [[IBM]], [[Digital Research|DR]], [[Novell]], [[Hewlett-Packard|HP]], [[JP Software]], ReactOS Contributors
| developer = [[AT&T Bell Laboratories]], [[MetaComCo]], [[Microsoft]], [[IBM]], [[Digital Research|DR]], [[Novell]], [[Hewlett-Packard|HP]], [[JP Software]], [[ReactOS]] Contributors
| released =
| released =
| latest release version =
| latest release version =
| latest release date =
| latest release date =
| operating system = [[Unix]], [[Unix-like]], [[V (operating system)|V]], [[DOS]], [[MSX-DOS]], [[FlexOS]], [[OS/2]], [[TRIPOS]], [[Microsoft Windows|Windows]], [[HP Multi-Programming Executive|MPE/iX]], [[Inferno (operating system)|Inferno]], [[ReactOS]], [[KolibriOS]], [[SymbOS]], [[DexOS]]
| operating system = [[Unix]], [[Unix-like]], [[V (operating system)|V]], [[DOS]], [[MSX-DOS]], [[FlexOS]], [[OS/2]], [[TRIPOS]], [[Microsoft Windows|Windows]], [[HP Multi-Programming Executive|MPE/iX]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]], [[ReactOS]], [[KolibriOS]], [[SymbOS]]
| platform = [[Cross-platform]]
| genre = [[Command (computing)|Command]]
| genre = [[Command (computing)|Command]]
| license =
| license =
| website =
| website =
}}
}}
The '''{{code|cd}}''' command, also known as '''{{code|chdir}}''' ('''ch'''ange '''dir'''ectory), is a [[Command-line interface|command-line]] [[shell (computing)|shell]]<!-- It is a sea of blue, but not a pleonasm: "command-line" is subordinated to "shell", not to "command" further --> command used to change the current [[working directory]] in various [[operating system]]s. It can be used in [[shell script]]s and [[batch file]]s.
The '''{{code|cd}}''' command, also known as '''{{code|chdir}}''' ('''ch'''ange '''dir'''ectory), is a [[Command-line interface|command-line]] [[shell (computing)|shell]]<!-- It is a sea of blue, but not a pleonasm: "command-line" is subordinated to "shell", not to "command" further --> command used to change the current [[working directory]] in various [[operating system]]s. It can be used in [[shell script]]s and [[batch file]]s.


== Implementations ==
== Implementations ==
The command has been implemented in operating systems such as [[Unix]], [[DOS]], [[IBM]] [[OS/2]],<ref>{{cite web|url=http://www.jatomes.com/Help/Os2Cmd.php#CD|title=JaTomes Help – OS/2 Commands|website=www.jatomes.com}}</ref> [[MetaComCo]] [[TRIPOS]],<ref>{{cite web|url=https://www.pagetable.com/docs/amigados_tripos/tripos_manuals.pdf |title=Introduction to Tripos |format=PDF |date= |accessdate=2020-05-01}}</ref> [[AmigaOS]]<ref>{{cite web|url=http://archive.org/details/1988-rugheimer-spanik-amigados-quick-reference|title=AmigaDOS quick reference|first1=Hannes|last1=Rügheimer|first2=Christian|last2=Spanik|date=May 1, 1988|publisher=Grand Rapids, Mi : Abacus|via=Internet Archive}}</ref> (where if a bare [[Path (computing)|path]] is given, cd is ''implied''), [[Microsoft Windows]], [[ReactOS]],<ref>{{cite web|url=https://github.com/reactos/reactos/blob/master/base/shell/cmd/internal.c|title=Reactos/reactos|website=GitHub}}</ref> and [[Linux]]. On [[MS-DOS]], it is available in versions 2 and later.<ref name="RUNNINGMSDOS">{{Cite book|author-last=Wolverton|author-first=Van|title=Running MS-DOS Version 6.22 (20th Anniversary Edition), 6th Revised edition|date=2003|publisher=[[Microsoft Press]]|isbn=0-7356-1812-7}}</ref> [[DR DOS 6.0]] also includes an implementation of the {{code|cd}} and {{code|chdir}} commands.<ref name="drdos6userguide">{{Cite web |url=https://www.4corn.co.uk/archive/docs/DR%20DOS%206.0%20User%20Guide-opt.pdf |title=DR DOS 6.0 User Guide Optimisation and Configuration Tips |access-date=2019-08-13 |archive-url=https://web.archive.org/web/20190930135943/http://www.4corn.co.uk/archive/docs/DR%20DOS%206.0%20User%20Guide-opt.pdf |archive-date=2019-09-30 |url-status=dead }}</ref> The command is also available in the [[Open-source software|open source]] MS-DOS [[emulator]] [[DOSBox]] and in the [[Unified Extensible Firmware Interface|EFI shell]].<ref name="EFI-Shells-and-Scripting">{{cite web
The command has been implemented in operating systems such as [[Unix]], [[DOS]], [[IBM]] [[OS/2]],<ref>{{cite web|url=http://www.jatomes.com/Help/Os2Cmd.php#CD|title=JaTomes Help – OS/2 Commands|website=www.jatomes.com|access-date=2019-08-11|archive-date=2019-04-14|archive-url=https://web.archive.org/web/20190414130029/http://www.jatomes.com/Help/Os2Cmd.php#CD|url-status=dead}}</ref> [[MetaComCo]] [[TRIPOS]],<ref>{{cite web|url=https://www.pagetable.com/docs/amigados_tripos/tripos_manuals.pdf |title=Introduction to Tripos |access-date=2020-05-01}}</ref> [[AmigaOS]]<ref>{{cite book|url=http://archive.org/details/1988-rugheimer-spanik-amigados-quick-reference|title=AmigaDOS quick reference|first1=Hannes|last1=Rügheimer|first2=Christian|last2=Spanik|date=May 1, 1988|publisher=Grand Rapids, Mi : Abacus|isbn=9781557550491|via=Internet Archive}}</ref> (where if a bare [[Path (computing)|path]] is given, cd is ''implied''), [[Microsoft Windows]], [[ReactOS]],<ref>{{cite web|url=https://github.com/reactos/reactos/blob/master/base/shell/cmd/internal.c|title=Reactos/reactos|website=GitHub|date=3 January 2022}}</ref> and [[Linux]]. On [[MS-DOS]], it is available in versions 2 and later.<ref name="RUNNINGMSDOS">{{Cite book|author-last=Wolverton|author-first=Van|title=Running MS-DOS Version 6.22 (20th Anniversary Edition), 6th Revised edition|date=2003|publisher=[[Microsoft Press]]|isbn=0-7356-1812-7}}</ref> [[DR DOS 6.0]] also includes an implementation of the {{code|cd}} and {{code|chdir}} commands.<ref name="drdos6userguide">{{Cite web |url=https://www.4corn.co.uk/archive/docs/DR%20DOS%206.0%20User%20Guide-opt.pdf |title=DR DOS 6.0 User Guide Optimisation and Configuration Tips |access-date=2019-08-13 |archive-url=https://web.archive.org/web/20190930135943/http://www.4corn.co.uk/archive/docs/DR%20DOS%206.0%20User%20Guide-opt.pdf |archive-date=2019-09-30 |url-status=dead }}</ref> The command is also available in the [[Open-source software|open source]] MS-DOS [[emulator]] [[DOSBox]] and in the [[Unified Extensible Firmware Interface|EFI shell]].<ref name="EFI-Shells-and-Scripting">{{cite web
| url = http://software.intel.com/en-us/articles/efi-shells-and-scripting/
| url = http://software.intel.com/en-us/articles/efi-shells-and-scripting/
| title = EFI Shells and Scripting
| title = EFI Shells and Scripting
| publisher = [[Intel]]
| publisher = [[Intel]]
| accessdate = 2013-09-25
| access-date = 2013-09-25
}}</ref> It is named {{code|chdir}} in [[Hewlett-Packard|HP]] [[HP Multi-Programming Executive|MPE/iX]].<ref>{{cite web|url=http://www.teamnaconsulting.com/compresources/pdfs/c01687363.pdf|title=MPE/iX Command Reference Manual}}</ref> The command is analogous to the [[Stratus Technologies|Stratus]] [[Stratus VOS|OpenVOS]] {{code|change_current_dir}} command.<ref>http://stratadoc.stratus.com/vos/19.1.0/r098-19/wwhelp/wwhimpl/common/html/r098-19.pdf</ref>
}}</ref> It is named {{code|chdir}} in [[Hewlett-Packard|HP]] [[HP Multi-Programming Executive|MPE/iX]].<ref>{{Cite web|url=http://www.teamnaconsulting.com/compresources/pdfs/c01687363.pdf|title=MPE/iX Command Reference Manual|access-date=2018-10-21|archive-date=2018-10-21|archive-url=https://web.archive.org/web/20181021232213/http://www.teamnaconsulting.com/compresources/pdfs/c01687363.pdf|url-status=dead}}</ref> The command is analogous to the [[Stratus Technologies|Stratus]] [[Stratus VOS|OpenVOS]] {{code|change_current_dir}} command.<ref>{{cite web |url=http://stratadoc.stratus.com/vos/19.1.0/r098-19/wwhelp/wwhimpl/common/html/r098-19.pdf |title= OpenVOS Commands Reference Manual |website=stratadoc.stratus.com |access-date=2020-09-12}}</ref>


{{code|cd}} is frequently included built directly into a command-line interpreter. This is the case in most of the [[Unix shell]]s ([[Bourne shell]], [[tcsh]], [[Bash (Unix shell)|bash]], etc.), <code>[[cmd.exe]]</code> on Microsoft [[Windows NT]]/[[Windows 2000|2000]]+ and [[Windows PowerShell]] on [[Windows 7]]+ and <code>[[COMMAND.COM]]</code> on DOS/ Microsoft [[Windows 3.x]]-[[Windows 9x|9x]]/[[Windows Millennium Edition|ME]].
{{code|cd}} is frequently included built directly into a command-line interpreter. This is the case in most of the [[Unix shell]]s ([[Bourne shell]], [[tcsh]], [[Bash (Unix shell)|bash]], etc.), <code>[[cmd.exe]]</code> on Microsoft [[Windows NT]]/[[Windows 2000|2000]]+ and [[Windows PowerShell]] on [[Windows 7]]+ and <code>[[COMMAND.COM]]</code> on DOS/ Microsoft [[Windows 3.x]]-[[Windows 9x|9x]]/[[Windows Millennium Edition|ME]].
Line 63: Line 65:


DOS maintains separate working directories for each [[Drive letter assignment|lettered drive]], and also has the concept of a current working drive. The {{code|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. {{code|C:}}; alternatively, {{code|cd}} with the {{code|/d}} switch may be used to change the working drive and that drive's working directory in one step.
DOS maintains separate working directories for each [[Drive letter assignment|lettered drive]], and also has the concept of a current working drive. The {{code|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. {{code|C:}}; alternatively, {{code|cd}} with the {{code|/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]].<ref>{{cite web|url=http://blogs.msdn.com/b/oldnewthing/archive/2010/10/11/10073890.aspx|title=Why does each drive have its own current directory?}}</ref>
Modern versions of Windows simulate this behaviour for backwards compatibility under [[CMD.EXE]].<ref>{{Cite web|url=https://devblogs.microsoft.com/oldnewthing/2010/10/11|title=October 11, 2010|website=The Old New Thing}}</ref>


Note that executing {{code|cd}} from the command line with no arguments has different effects in different operating systems. For example, if {{code|cd}} is executed without arguments in DOS, OS/2, or Windows, the current [[working directory]] is displayed (equivalent to Unix <code>[[pwd]]</code>). If {{code|cd}} is executed without arguments in Unix, the user is returned to the home directory.
Note that executing {{code|cd}} from the command line with no arguments has different effects in different operating systems. For example, if {{code|cd}} is executed without arguments in DOS, OS/2, or Windows, the current [[working directory]] is displayed (equivalent to Unix <code>[[pwd]]</code>). If {{code|cd}} is executed without arguments in Unix, the user is returned to the home directory.
Line 71: Line 73:
== Options ==
== Options ==
=== Unix, Unix-like ===
=== Unix, Unix-like ===
* {{code|cd}} by itself or {{code|cd ~}} will always put you in your home directory.
* {{code|cd}} by itself or {{code|cd ~}} will always put the user in their home directory.
* {{code|cd .}} will leave you in the same directory you are currently in (i.e. your current directory won't change). This can be useful if your shell's internal code can't deal with the directory you are in being recreated; running {{code|cd .}} will place your shell in the recreated directory.
* {{code|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 {{code|cd .}} will place their shell in the recreated directory.
* {{code|cd ~username}} will put you in username's home directory.
* <code>cd ~''username''</code> will put the user in the username's home directory.
* {{code|cd dir}} (without a {{code|/}}) will put you in a subdirectory; for example, if you are in {{code|/usr}}, typing {{code|cd bin}} will put you in {{code|/usr/bin}}, while {{code|cd /bin}} puts you in {{code|/bin}}.
* {{code|cd dir}} (without a {{code|/}}) will put the user in a subdirectory; for example, if they are in {{code|/usr}}, typing {{code|cd bin}} will put them in {{code|/usr/bin}}, while {{code|cd /bin}} puts them in {{code|/bin}}.
* {{code|cd ..}} will move you up one directory. So, if you are {{code|/usr/bin/tmp}}, {{code|cd ..}} moves you to {{code|/usr/bin}}, while {{code|cd ../..}} moves you to {{code|/usr}} (i.e. up two levels). You can use this indirection to access subdirectories too. So, from {{code|/usr/bin/tmp}}, you can use {{code|cd ../../local}} to go to {{code|/usr/local}}.
* {{code|cd ..}} will move the user up one directory. So, if they are {{code|/usr/bin/tmp}}, {{code|cd ..}} moves them to {{code|/usr/bin}}, while {{code|cd ../..}} moves them to {{code|/usr}} (i.e. up two levels). The user can use this indirection to access subdirectories too. So, from {{code|/usr/bin/tmp}}, they can use {{code|cd ../../local}} to go to {{code|/usr/local}}
* {{code|cd -}} will switch you to the previous directory. For example, if you are in {{code|/usr/bin/tmp}}, and go to {{code|/etc}}, you can type {{code|cd -}} to go back to {{code|/usr/bin/tmp}}. You can use this to toggle back and forth between two directories.
* {{code|cd -}} will switch the user to the previous directory. For example, if they are in {{code|/usr/bin/tmp}}, and go to {{code|/etc}}, they can type {{code|cd -}} to go back to {{code|/usr/bin/tmp}}. The user can use this to toggle back and forth between two directories without [[pushd and popd]].


=== DOS, OS/2, Windows, ReactOS ===
=== DOS, OS/2, Windows, ReactOS ===
Line 95: Line 97:


== See also ==
== See also ==
{{Portal|Free and open-source software}}
*[[Directory structure]]
*[[Directory structure]]
*[[pushd and popd]]
*[[pushd and popd]]
Line 117: Line 118:
{{Unix commands}}
{{Unix commands}}
{{Windows commands}}
{{Windows commands}}
{{Portalbar|Free and open-source software}}


[[Category:Internal DOS commands]]
[[Category:Internal DOS commands]]
[[Category:File system directories]]
[[Category:File system directories]]
[[Category:Inferno (operating system) commands]]
[[Category:IBM i Qshell commands]]
[[Category:MSX-DOS commands]]
[[Category:OS/2 commands]]
[[Category:OS/2 commands]]
[[Category:ReactOS commands]]
[[Category:Windows administration]]
[[Category:Windows administration]]
[[Category:Standard Unix programs]]
[[Category:Standard Unix programs]]

Latest revision as of 15:49, 28 May 2024

cd / chdir
Developer(s)AT&T Bell Laboratories, MetaComCo, Microsoft, IBM, DR, Novell, HP, JP Software, ReactOS Contributors
Operating systemUnix, Unix-like, V, DOS, MSX-DOS, FlexOS, OS/2, TRIPOS, Windows, MPE/iX, Plan 9, Inferno, ReactOS, KolibriOS, SymbOS
PlatformCross-platform
TypeCommand

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.

Implementations[edit]

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[edit]

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.

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.txt
user@wikipedia:~$ cd games
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[edit]

Unix, Unix-like[edit]

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

DOS, OS/2, Windows, ReactOS[edit]

  • no attributes print the full path of the current directory.
  • -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.

Interpreters other than an operating systems shell[edit]

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[edit]

References[edit]

  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[edit]

External links[edit]