Using cd

cd is used to change your current working directory. It can be used in a variety of useful ways. (You may also use chdir instead; they are the exact same.)

First form

For almost all UNIX shells, the basic usage of cd is as follows:

Typing: Will:
cd Place you in your home directory, ${HOME}
cd NEW_DIR Place you in the directory called NEW_DIR
cd - Place you in the directory you were just in, ${OLDPWD}

zsh adds some useful functionality to cd

First, if zsh doesn't find NEW_DIR in the current directory (and NEW_DIR isn't an absolute path), zsh looks at the shell variable cdpath. It looks for a subdirectory NEW_DIR in each directory of cdpath. If zsh sees NEW_DIR, it goes there.

Furthermore, if you like, you can store the FULL pathname of a directory in a shell variable. You can then type cd VAR and go into the directory contained in the shell variable VAR. For example:

  lyric >  XDIR=/usr/lib/X11
  lyric >  cd XDIR
  lyric >  pwd
  lyric >

Why is this useful? Why not just type cd $XDIR each time? The reason is that you get the added benefit of zsh making XDIR a named directory. Essentially, this means that the directory can get special treatment in prompt expansion, and various builtins.

Second Form

The second form of cd is handy! Type in


and zsh replaced any occurance of OLD in the current directory with NEW, and then cd's into it.

A simple example:

  lyric > pwd
  lyric > cd 2.2 2.0.46
  lyric > 

Third Form

The third form pulls an entry from the directory stack, and switches to that directory (n is an interger):

cd +n
cd -n

To see the directory stack, type dirs -v.

Giving an arg of '+n' will extract the nth item from the directory stack, counting from the left (top). Using a '-' counts from the right (bottom).

If you would like to switch the meanings of '+' and '-', turn on the shell option PUSHD_MINUS.

Arguments to cd

All forms of the cd builtin can take a few arguments:

cd -s DIR Don't cd into DIR if DIR's path contains symlinks
cd -P DIR Resolve all symlinks to their true values before changing to DIR
cd -L DIR Symlinks are followed, ignoring the CHASE_LINKS option)

Note: Turning on the shell option CHASE_LINKS is the same as the -P argument to cd.

Related Items