Vous êtes sur la page 1sur 5

cd (command)

From Wikipedia, the free encyclopedia

The c dcommand, also known as c h d i r(change directory), is a command-line OS shell command used 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. The system call that effects the command in most operating systems is chdir(2) that is defined by POSIX.

Contents
1 2 3 4 5 6 7 Usage Options (version specific) How it works Interpreters other than an operating systems shell See also References External links

Usage
A directory is a logical section of a file system used to hold files. Directories may also contain other directories. The c dcommand 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 (t e x t . t x t ) 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 l sfollowed by c dg a m e smight produce the following transcript:

u s e r @ w i k i p e d i a : ~ $l s w o r k r e p o r t sg a m e se n c y c l o p e d i at e x t . t x t u s e r @ w i k i p e d i a : ~ $c dg a m e s u s e r @ w i k i p e d i a : ~ / g a m e s $

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 : \ >d i r w o r k r e p o r t s g a m e s e n c y c l o p e d i a t e x t t x t C : \ >c dg a m e s C : \ g a m e s >

< D I R > < D I R > < D I R >

W e dO c t9 t h 9 : 0 1 T u eO c t8 t h 1 4 : 3 2 M o nO c t1 s t 1 0 : 0 5 1 9 0 3T h uO c t 1 0 t h 1 2 : 4 3

DOS maintains separate working directories for each lettered drive, and also has the concept of a current working drive. The c dcommand 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, c d

with the / dswitch 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.[1] Note that executing c dfrom the command line with no arguments has different effects in different operating systems. For example, if c dis executed without arguments in DOS, OS/2, or Windows, the current working directory is displayed. If c d is executed without arguments in Unix, the user is returned to the home directory. Executing the c dcommand 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 c dcommand. This is because in Unix, the script is usually executed within a subshell.

Options (version specific)


n oa t t r i b u t e sReturn

to the home directory (UNIX) or print the full path of the current directory (DOS and Windows) pPrint the final directory stack, just like dirs. l' ~ '(UNIX only) or ' ~ n a m e '(UNIX only) in the output is expanded explicitly to home or the pathname of the home directory for user name. nEntries are wrapped before they reach the edge of the screen. ventries are printed one per line, preceded by their stack positions. c d \(DOS and Windows only) returns to the root dir. Consequently, command c d \ s u b d i ralways takes the user to the named subdirectory on the root directory, regardless of where they are located when the command is issued. Application of some options (UNIX)[2] "cd" by itself or cd ~ will always put you in your home directory. "cd ." will leave you in the same directory you are currently in (i.e. your current directory won't change) "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 (UNIX only). 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
c dis

frequently included built directly into a command-line interpreter. This is the case in most of the Unix shells (Bourne shell, tcsh, bash, etc.), c m d . e x eand Windows PowerShell on Windows and C O M M A N D . C O Mon DOS. Command line shells on Windows usually use the Windows API to change the current working directory, whereas on Unix systems c dcalls the c h d i r ( )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 c d 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 c dcommand (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.

Interpreters other than an operating systems shell


In the File Transfer Protocol, the respective command is spelled C W Din the control stream, but is available as c din most client command-line programs. Some clients also have the l c dfor changing the working directory locally. The command also pertains to command-line interpreters of various application software.

See also
Directory structure pushd and popd chroot List of command-line interpreters

References
1. ^ Why does each drive have its own current directory?

(http://blogs.msdn.com/b/oldnewthing/archive/2010/10/11/10073890.aspx) 2. ^ Unix Change Directory Commands (http://unix-simple.blogspot.com/2006/12/unixchange-directory-commands.html)

External links
Windows XP > Command-line reference A-Z > Chdir (Cd) (http://technet.microsoft.com/en-us/library/bb490875.aspx) from Microsoft TechNet c d( h t t p : / / w w w . o p e n g r o u p . o r g / o n l i n e p u b s / 9 6 9 9 9 1 9 7 9 9 / u t i l i t i e s / c d . h t m l ) : change the working directory Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group Retrieved from "http://en.wikipedia.org/w/index.php? title=Cd_(command)&oldid=597089505" Categories: Windows commands Internal DOS commands File system directories OS/2 Windows administration Standard Unix programs Unix SUS2008 utilities This page was last modified on 25 February 2014 at 16:43. Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc., a nonprofit organization.

Vous aimerez peut-être aussi