cd (command)

cd (command)

cd, sometimes also available as chdir (change directory), is a command line command to change the current working directory in operating systems such as Unix, DOS, OS/2, AmigaOS (where if a bare path is given, cd is implied), Windows, and Linux. It is also available for use in shell scripts and batch files.

chdir(2) is the system call which changes the working directory,[1] as defined by POSIX. CHDIR() is also a Visual Basic function which changes the working directory.

Contents

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.


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.[2]

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. 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 (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.[where?]
  • -l [where?] '~' (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.[where?]
  • -v entries are printed one per line, preceded by their stack positions.[where?]
  • 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)[3]

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 shells (Bourne shell, tcsh, bash, etc.), cmd.exe and Windows PowerShell on Windows and COMMAND.COM on DOS.

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.

Unlike the Windows API or POSIX C function, neither the Java programming language nor the Java Virtual Machine supports chdir() directly; a change request 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 ).[4]

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.

See also

References

General references:

Specific references:


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Command \x26 Conquer — Command Conquer Saltar a navegación, búsqueda Command Conquer es una saga de videojuegos de estrategia en tiempo real, originalmente creada por Westwood Studios y posteriormente adquirida por Electronic Arts. Command Conquer es considerado un… …   Wikipedia Español

  • Command & Conquer — Жанры Стратегия реального времени Разработчики Westwood Studios EA Los Angeles Victory Games Издатели Virgin Interactive Nintendo Sega Electronic Arts Платформы IBM PC …   Википедия

  • Command & Conquer: Red Alert 3 — Red Alert 3 cover art Developer(s) EA Los Angeles Publisher(s) Elect …   Wikipedia

  • Command & Conquer — Command Conquer (abgekürzt C C oder CnC, engl. für kommandieren und erobern) ist ein von den Westwood Studios entwickelte Spielereihe. Mit seiner unkomplizierten Steuerung, der spannenden Geschichte und seiner Multiplayerfunktion erwarb sich… …   Deutsch Wikipedia

  • Command & Conquer: Alarmstufe Rot — Command Conquer: Alarmstufe Rot (Originaltitel: Red Alert) ist ein von den Westwood Studios entwickeltes Echtzeit Strategiespiel und der zeitliche (in Hinsicht auf das Erscheinungsdatum), nicht aber handlungsbezogene Nachfolger von Command… …   Deutsch Wikipedia

  • Command & Conquer: Alarmstufe Rot - Yuris Rache — Command Conquer: Alarmstufe Rot (Originaltitel: Red Alert) ist ein von den Westwood Studios entwickeltes Echtzeit Strategiespiel und der zeitliche (in Hinsicht auf das Erscheinungsdatum), nicht aber handlungsbezogene Nachfolger von Command… …   Deutsch Wikipedia

  • Command & Conquer: Alarmstufe Rot 2 — Command Conquer: Alarmstufe Rot (Originaltitel: Red Alert) ist ein von den Westwood Studios entwickeltes Echtzeit Strategiespiel und der zeitliche (in Hinsicht auf das Erscheinungsdatum), nicht aber handlungsbezogene Nachfolger von Command… …   Deutsch Wikipedia

  • Command & Conquer: Alarmstufe Rot 3 — Command Conquer: Alarmstufe Rot (Originaltitel: Red Alert) ist ein von den Westwood Studios entwickeltes Echtzeit Strategiespiel und der zeitliche (in Hinsicht auf das Erscheinungsdatum), nicht aber handlungsbezogene Nachfolger von Command… …   Deutsch Wikipedia

  • Command & Conquer: Alarmstufe Rot 3: Der Aufstand — Command Conquer: Alarmstufe Rot (Originaltitel: Red Alert) ist ein von den Westwood Studios entwickeltes Echtzeit Strategiespiel und der zeitliche (in Hinsicht auf das Erscheinungsdatum), nicht aber handlungsbezogene Nachfolger von Command… …   Deutsch Wikipedia

  • Command and Conquer — Command Conquer (abgekürzt C C oder CnC, engl. für kommandieren und erobern) ist ein von den Westwood Studios entwickelte Spielereihe. Mit seiner unkomplizierten Steuerung, der spannenden Geschichte und seiner Multiplayerfunktion erwarb sich… …   Deutsch Wikipedia

  • Command & Conquer — Command Conquer Жанры Стратегия реального времени Разработчики Westwood EA Loas Angeles Издатели Virgin Interactive Sega Electronic Arts Платформы IBM PC PlayStation …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”