Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

cd - change working directory

&pagelevel(4)&pagelevel

The built-in cd command in the POSIX shell sh makes the specified directory your current working directory.
The cd command is rejected in a restricted shell.

Syntax


Format 1: cd[ directory]
Format 2: cd -
Format 3: cd old new



Format 1: cd[ directory]

Change directory using CDPATH

directory

Name of the directory that is to become your current working directory. You must have
execute permission for this directory. If you specify a relative or absolute path name for
directory, you must have execute permission for all the directories which make up this
path name.

If the name of the specified directory begins with one of the following characters, the
command looks for the directory without reference to the CDPATH environment variable
(see POSIX environment variables):


/means that the search begins in the root directory.
./means that the search begins in the current directory.
../means that the search begins in the parent directory.


If the name of the specified directory does not begin with any of the above characters, cd evaluates the CDPATH environment variable:

  • If the CDPATH variable has not been defined or is null, cd looks for the specified directory relative to the current working directory.

  • If the CDPATH variable has been assigned a value, cd looks for the specified directory sequentially in the directories whose paths are defined in the CDPATH variable. On finding the directory, cd writes the absolute path name of this directory on standard output before switching to it.

directory not specified:
The cd command puts you in your home directory. The home directory is identical to the login directory unless there is a different path name assigned to the shell variable HOME.


Format 2: cd  -

Specifying the - as an operand has the same effect as the command cd "$OLDPWD" && pwd which changes to the last active directory and writes its name.


Format 3: cd old new

Change directory with text substitution

cd substitutes the string new for the string old in the current directory name (PWD) and tries to change to this new directory.

Error

sh: file: not found
The specified directory does not exist. You can verify this with ls -l.


sh: file: not a directory
Your argument is not a directory. This can also be verified with ls -l.


file: permission denied
You do not have execute permission for the specified directory.
If you have specified a relative or absolute path name for directory, you do not have execute permission for one of the directories which make up this path name.


rsh: cd: restricted

cd has been rejected because you are working in a restricted shell.

Variable

HOME

contains the absolute path name of your home directory.

CDPATH

You can assign to CDPATH the absolute path names of directories that cd is to search.
By default this variable is undefined.

OLDPWD

Path name of the previous directory used by cd -.

PWD

Path name of the current directory. This name is set by cd following the change to this directory.

Locale

The following environment variables affect the execution of cd:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments). LC_CTYPE governs character classes, character conversion (shifting) and the behavior of character classes in regular expressions.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1


The following entry makes the subdirectory dates the current directory:

$ cd dates
$ pwd
/home/rose/dates

Example 2


User rose has redefined the CDPATH environment variable. She now wishes to change to her subdirectory usr, but with the following commands ends up in the directory /usr instead:

$ echo $CDPATH
/:/home/rose/dates:.
$ pwd
/home/rose
$ ls -l
drwx--x--x 2 ROSE        144 Feb 28 12:32 usr
drwx--x--x 2 ROSE        192 Feb 28 11:51 dates
-rw------- 1 ROSE      11734 Mar  7 16:22 tests
.
.
.
$ cd usr
$ pwd
/usr

The usr directory is first looked for in the directories whose path names are assigned to the CDPATH variable. In this case, CDPATH contains a / for the root directory as the first path name. The current directory is the last to be searched by cd.

User rose can prevent cd from evaluating the CDPATH environment variable by formulating the command in the following way:

$ cd ./usr
$ pwd
/home/rose/usr



See also

pwd

chdir() [4]