cd and pwd: move between directories
cd changes the current working directory and pwd prints it. Together they are how you move around the file tree.
Examples
pwd # where am I? e.g. /home/anna
cd /var/log # absolute path: from the root
cd nginx # relative path: /var/log/nginx
cd .. # one level up
cd ../.. # two levels up
cd # your home directory
cd ~ # the same
cd ~bob # bob's home directory
cd - # back to the previous directory (and print it)
cd "My Documents" # quote names with spaces
Directory stack: pushd and popd
In bash and zsh you can keep a stack of directories and jump back to them:
pushd /etc/nginx # go to /etc/nginx and remember where you were
pushd /var/log # go on to /var/log
dirs -v # show the stack
popd # back to /etc/nginx
popd # back to where you started
pwd and symbolic links
If you reached a directory through a symbolic link, pwd shows the path you used. pwd -P shows the physical path with links resolved. cd -P resolves links while changing directory.
CDPATH
The CDPATH variable lists directories that cd searches for relative names, like PATH for commands. With CDPATH=.:~/projects, typing cd website from anywhere jumps to ~/projects/website. Do not export it in scripts; it can make cd go somewhere unexpected.
Why cd is not a program
cd is built into the shell, because a separate program cannot change the directory of the shell that started it. That is also why cd inside a script does not change the directory of your terminal: the script runs in its own shell. In scripts, always check that cd worked before doing anything destructive:
cd /srv/app/tmp || exit 1
rm -f ./*.tmp