cd

Change Directory - change the current working Folder.

Syntax 
      cd [ -qsLP ] [ arg ]
      cd [ -qsLP ] old new
      cd [ -qsLP ] {+|-}n

Key
   arg  Change the current directory to arg, or to the value of $HOME if arg is not specified.

   old new Substitute the string new for the string old in the name of the current directory, and
           attempt to change to this new directory.

   -P   Symbolic links are resolved to their true values (also true if the CHASE_LINKS option is set).
        (see also the -P option to the set builtin command)

   -L   Symbolic links are retained in the directory (and not resolved) regardless of the
        state of the CHASE_LINKS option

   -q   Quiet.

   -s   Do not change the current directory if the given pathname contains symlinks.

    n   Extract an entry from the directory stack, and change to that directory.

If a dir is given, changes the shell’s working directory to dir. If not, changes to HOME (shell variable).

In the first form, If arg is ‘-’, change to the previous directory. Otherwise, if arg begins with a slash, attempt to change to the directory given by arg. This does not stack, so issuing CD - repeatedly will just toggle between two directories, to go back further use pushd/popd.

If arg does not begin with a slash, the behaviour depends on whether the current directory ‘.’ occurs in the list of directories contained in the shell parameter cdpath. If it does not, first attempt to change to the directory arg under the current directory, and if that fails but cdpath is set and contains at least one element attempt to change to the directory arg under each component of cdpath in turn until successful.

The previous directory is also returned by $OLDPWD

./ or just . is shorthand for the current directory.

If ‘.’ occurs in cdpath, then cdpath is searched strictly in order so that ‘.’ is only tried at the appropriate point. The order of testing cdpath is modified if the option POSIX_CD is set, as described in the documentation for the option. If no directory is found, the option CDABLE_VARS is set, and a parameter named arg exists whose value begins with a slash, treat its value as the directory. In that case, the parameter is added to the named directory hash table.

The third form of cd extracts an entry from the directory stack, and changes to that directory. An argument of the form ‘+n’ identifies a stack entry by counting from the left of the list shown by the dirs command, starting with zero. An argument of the form ‘-n’ counts from the right. If the PUSHD_MINUS option is set, the meanings of ‘+’ and ‘-’ in this context are swapped. If the POSIX_CD option is set, this form of cd is not recognised and will be interpreted as the first form.

If the -q (quiet) option is specified, the hook function chpwd and the functions in the array chpwd_functions are not called. This is useful for calls to cd that do not change the environment seen by an interactive user.

CDPATH

The shell variable CDPATH provides a useful feature, this variable is similar to PATH but it sets up a list of paths where cd will search for subdirectories. CDPATH can be set on the command line for use in the current session, or in the profile for permanent use, the list of paths must be colon separated (:)

$ CDPATH=".:~:~/Library"

If dir begins with a slash (/), then CDPATH is not used.

If a non-empty directory name from CDPATH is used, or if - is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output.

The return value is true if the directory was successfully changed; false otherwise.

cd is a Zsh shell builtin, see also the bash cd builtin.

Examples

Move to a subfolder named "Documents":

$ cd Documents

Move to the fonts folder:

$ cd /library/fonts
$ pwd
cd /library/fonts

Change to another folder:

$ cd ~/music
$ pwd
/Users/kate/music

Move up to the parent directory (/Users/kate/):

cd ..

Move up two levels:

$ cd ../..

Move to the top of the boot volume:

$ cd /

Move to your home directory:

$ cd ~
or just:
$ cd

Move to a folder when the folder name includes spaces, parentheses or any other punctuation:

$ cd 'AppleWorks User Data'

“In times of change, learners inherit the earth, while the learned find themselves equipped to deal with a world that no longer exists” ~ Eric Hoffer

Related

Local man page: cd - Command line help page on your local machine, or type: help cd.
popd - Restore the previous value of the current directory.
pushd - Save and then change the current directory.
pwd - Print Working Directory.


 
Copyright © 1999-2026 SS64.com
Some rights reserved