TExcelTAD
Home › Linux & Bash › cd & pwd: Navigate

cd & pwd: Navigate

Every shell has a current working directory: the folder that relative paths start from. pwd (print working directory) shows where you are, and cd (change directory) moves you somewhere else.

bash · try it yourself
pwd
mkdir -p projects/website/assets
cd projects
pwd
cd website/assets
pwd
/home/learner
/home/learner/projects
/home/learner/projects/website/assets

Absolute and relative paths

Path Kind Meaning
/home/learner/projects absolute Starts at the root /, works from anywhere
projects/website relative Starts at the current directory
. relative The current directory
.. relative The parent directory
~ shortcut Your home directory (/home/learner here)
~/projects shortcut A path inside your home directory
bash · try it yourself
mkdir -p ~/music/jazz ~/music/rock
cd ~/music/jazz
pwd
cd ..
pwd
cd ../music/rock
pwd
cd /
pwd
cd ~
pwd
/home/learner/music/jazz
/home/learner/music
/home/learner/music/rock
/
/home/learner

.. can be chained (../..) and mixed with names (../music/rock: up one level, then down again).

Shortcuts for cd

Command Goes to
cd Your home directory
cd ~ Your home directory
cd - The previous directory (and prints it)
cd ~/Downloads A folder in your home

cd - is great for jumping back and forth between two places:

bash · try it yourself
mkdir -p src/app logs
cd src/app
cd ~/logs
cd -
pwd
cd -
/home/learner/src/app
/home/learner/src/app
/home/learner/logs

Bash remembers the previous location in the variable $OLDPWD, and the current one in $PWD.

Errors you'll meet

bash · try it yourself
cd reports
touch notes.txt
cd notes.txt
cd "my folder"
echo "still in $PWD"
/w/main.sh: line 1: cd: reports: No such file or directory
/w/main.sh: line 3: cd: notes.txt: Not a directory
/w/main.sh: line 4: cd: my folder: No such file or directory
still in /home/learner
  • No such file or directory: the path doesn't exist (check spelling and case).
  • Not a directory: you tried to enter a file.
  • A failed cd leaves you where you were. In scripts that can be dangerous: if cd build fails and the next line is rm -rf *, you delete the wrong files. Write cd build || exit 1.

Directory stack: pushd and popd

pushd changes directory and remembers the old one on a stack; popd returns to it. Useful in scripts that visit several places:

bash · try it yourself
mkdir -p site/css site/js
cd site
pushd css > /dev/null
pwd
pushd ../js > /dev/null
pwd
dirs
popd > /dev/null
pwd
popd > /dev/null
pwd
/home/learner/site/css
/home/learner/site/js
~/site/js ~/site/css ~/site
/home/learner/site/css
/home/learner/site

dirs shows the stack, current directory first (with ~ for your home). The > /dev/null hides the stack that pushd/popd print by default.

cd in scripts

A script runs in its own process, so a cd inside a script doesn't move the terminal you started it from (see the Scripts lesson). If you only need another directory for one command, use a subshell in parentheses: (cd build && make) changes directory just for the commands inside.

Tip: Paths with spaces need quotes: cd "Monthly Reports". Pressing Tab completes the name and adds the escaping for you.

Test yourself

You are in /home/learner/projects/web. Where does cd ../.. take you?

  1. /home
  2. /home/learner
  3. /home/learner/projects
  4. /

What does cd - do?

  1. Goes to the root directory
  2. Goes to the home directory
  3. Returns to the previous directory
  4. Prints an error

Why is cd build || exit 1 safer than cd build in a script?

  1. It is faster
  2. It stops the script if the directory doesn't exist, so later commands don't run in the wrong place
  3. It creates the directory
  4. It is required syntax