Linux Command Reference ยท Files and Directories
Linux cd command
Reference for the Bash cd builtin: change the working directory, use ~, - and .., understand CDPATH, -P and -L, and avoid common mistakes in scripts.
cd changes the current working directory of the shell. It is a shell builtin, not a program, and it has to be: a separate process could only change its own directory, never the one of the shell that started it.
With no argument cd goes to $HOME. With - it returns to the previous directory, which is stored in OLDPWD. Relative paths are resolved from the current directory, and .. means the parent.
cd [-L|-P] [directory] cd -
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
cd [-L|-P[-e]] [directory]
| Name | Description |
|---|---|
directory |
Target directory. If omitted, $HOME is used. - means $OLDPWD. |
-L |
Follow symbolic links logically (default): .. removes the last path component as written. |
-P |
Resolve symlinks first, so .. is the real parent directory. |
-e |
With -P, return status 1 if the working directory cannot be determined afterwards. |
Returns: 0 if the directory changed, non-zero otherwise (missing directory, not a directory, no execute permission, HOME unset).
Variables set: PWD (new directory) and OLDPWD (previous one).
Usage notes
- Execute permission is required. To enter a directory you need the
xbit on it, not onlyr. Withoutx,cdfails with "Permission denied". cd -prints the new directory to standard output (it is the one case wherecdis not silent), which is handy but unwanted in scripts. Redirect it withcd - >/dev/null.CDPATHworks likePATHforcd: relative names are looked up in each listed directory. It is convenient interactively, but exporting it surprises scripts. IfCDPATHis set,cdmay print the chosen directory.- Check the result in scripts. Write
cd "$dir" || exit 1. Otherwise a failedcdlets the rest of the script run in the wrong place, for example deleting files in the wrong directory. - Quote the argument.
cd $dirbreaks on spaces and glob characters;cd "$dir"does not. A name that starts with a dash needs--(cd -- -weird). - Tilde expansion:
cd ~is home,cd ~useris another user's home,cd ~+is the current directory, andcd ~-is the previous one. - Subshell trick:
( cd dir && command )runs a command elsewhere without changing the directory of the shell afterwards. shopt -s autocdlets you type a directory name alone to change into it.shopt -s cdspellcorrects small typos interactively.- A directory removed under you: if the current directory is deleted,
pwdstill prints the old name but relative operations fail until youcdsomewhere valid.
Related
pwd,ls,realpath,mkdir- Lessons: Navigating the filesystem, Hard and symbolic links