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.
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 |
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:
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
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
cdleaves you where you were. In scripts that can be dangerous: ifcd buildfails and the next line isrm -rf *, you delete the wrong files. Writecd 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:
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?
- /home
- /home/learner
- /home/learner/projects
- /
What does cd - do?
- Goes to the root directory
- Goes to the home directory
- Returns to the previous directory
- Prints an error
Why is cd build || exit 1 safer than cd build in a script?
- It is faster
- It stops the script if the directory doesn't exist, so later commands don't run in the wrong place
- It creates the directory
- It is required syntax