Where to start First steps track · 15 stages
Linux Labs

cd — changing directory

4 min bash · zsh

Moving around the filesystem: cd, cd -, cd ~, cd .., pushd/popd, CDPATH.

Cheat sheet

8 commands Printable version
  • cd dir go into a directory
  • cd .. one level up
  • cd ~ home
  • cd - back to the previous directory
  • pushd dir go there, remembering the old directory on a stack
  • popd go back to the previous one from the stack
  • dirs -v show the directory stack
  • cd "path with space" a path with a space — always quote it

Overview

cd (change directory) is a shell builtin: it changes the current working directory. It is not a separate program but a builtin of bash itself (which is why there's no man cd — use help cd).

pwd shows where you are now; cd with no arguments takes you home.

Paths: . is the current directory, .. is the parent, ~ is home (/home/user), / is root, - is the previous directory.

Options

4

cd has few flags — it's a builtin, not a separate program with a large --help.

Option What it does Example
- Go to the previous working directory ($OLDPWD) and print it. cd -
-L Follow symlinks logically (honor `..` along the symlink's path). The default behavior. cd -L /var/www/current
-P Resolve symlinks physically — go to the real directory; `pwd` shows the actual path. cd -P /var/www/current
-e With -P, exit with an error if the resulting directory could not be determined. cd -Pe /var/www/current

Nothing found. Try a shorter query.

Recipes

2

Examples

bash
cd /etc                 # go to an absolute path
cd logs                 # a relative one (from the current directory)
cd ..                   # one level up
cd ../..                # two levels up
cd                      # to the home directory (same as cd ~)
cd ~                    # home directory
cd ~user                # another user's home directory
cd -                    # go back to the PREVIOUS directory (toggle back and forth)
cd /var/log && ls       # go there and run a command right away

cd - is the most common trick: hopping between two directories (it prints where it went).

Tricks

bash
pushd /var/log          # go there and REMEMBER the old directory on a stack
pushd /etc/nginx        # another one — the stack grows
popd                    # go back to the previous one from the stack
dirs -v                 # show the directory stack with numbers

# CDPATH — "quick bases" for cd (like PATH, but for directories):
export CDPATH=".:/var:$HOME/projects"
cd nginx                # finds /var/log/nginx even if you're not there

In bash you can turn on shopt -s autocd — then typing a bare path without cd goes there directly. And cd supports Tab completion for names.

Troubleshooting

4

cd: too many arguments

cd takes one path. If the directory name has a space, quote it: cd "My Folder", or escape it: cd My\ Folder.

cd: not a directory

The target path is a file, not a directory. Check with ls -ld path.

cd: permission denied

No x (enter) permission on the directory. You need the execute bit — see chmod-chown.

"I cd'd inside a script, and afterward I'm back in the old directory"

Every process/script has its own working directory. cd inside a bash script does not change the parent shell's directory — that is expected.

Terms

1
home directory
A user's personal directory as set in /etc/passwd, usually /home/<name>; for root it is /root. The shell expands ~ to it, and the HOME variable holds its path. Also appears in: useradd, usermod, Users and groups, chage and 4 more

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, terminal copy-paste.
  • mc — A two-panel terminal file manager: navigation, copying, an editor, SFTP.
  • env-vars — env, export, $PATH, ~/.bashrc vs profiles; why "command not found".
Loading…