Module 1 · First steps in the terminal
Moving around with cd
Changing the working directory: cd to a path, up one level, back home, back to where you just were, and what to do when it says No such file or directory.
What you will learn
- Move to any directory by path.
- Use the shortcuts `cd`, `cd ..` and `cd -`.
- Understand why `cd` is built into the shell.
cd — *change directory* — moves your shell to another place in the tree. From then on the prompt shows the new location, pwd prints it, and plain ls lists it. It is the only navigation command you need, and it has three shortcuts you will use dozens of times a day.
~% cd /etc
etc% pwd
/etc
etc% cd init.d
init.d% pwd
/etc/init.d
init.d% cd ..
etc% cd
~% pwd
/root
~% cd -
/etc
etc%
The four moves
| Command | Goes to |
|---|---|
cd /etc | Exactly that path, from anywhere. |
cd init.d | The directory init.d inside the current one. Fails if you are elsewhere. |
cd .. | The parent: one level up. cd ../.. goes up two. |
cd | Home, wherever you are. Same as cd ~. |
cd - | The directory you were in just before. It prints where it landed. |
cd - is the one people discover late and then cannot live without: it toggles between two places. Working in /var/log and /etc at the same time? Type cd - to hop, and cd - again to hop back. .. is the parent directory and works in any path, not only alone: cd ../sbin from /usr/bin lands in /usr/sbin. Lesson 6 is entirely about these dots.
When it refuses
~% cd lab/l5/dcos
sh: cd: can't cd to lab/l5/dcos: No such file or directory
~% cd lab/l5/docs
docs%
No such file or directory is almost always a typo, or a relative name typed from the wrong place. Check with ls where you are: if ls lab/l5 shows docs, then cd lab/l5/docs must work. The other refusal you may meet is Not a directory: you tried to enter a file. Files have no inside; cd is for directories only. As root you will never see Permission denied, but ordinary users do when a directory lacks the x bit for them — module 5 explains.
Why cd is not a program
ls is a program on disk, /bin/ls. cd is not: type cd answers cd is a shell builtin. The reason is a rule of Unix processes: a program can change its *own* working directory but never its parent's. If cd were a separate program, it would run, move itself, exit — and your shell would still be where it was. So the shell implements cd internally. The same is true of pwd, echo and exit; type NAME tells you which kind any command is.
Commands in this lesson
| Command | What it does |
|---|---|
cd PATH | Move to PATH, absolute or relative. |
cd .. | Move to the parent directory. |
cd | Move to your home directory. |
cd - | Move back to the previous directory. |
type NAME | Say whether NAME is a builtin or a program, and where. |
Quiz
You are in `/usr/bin`. Where does `cd ..` take you?
- `/`
- `/usr`
- `/root`
What does `cd` with no argument do?
- Nothing
- Goes to the root directory `/`
- Goes to your home directory
You were in `/etc`, then ran `cd /tmp`. What does `cd -` do?
- Goes back to `/etc` and prints it
- Goes to the parent of `/tmp`
- Goes home
Why is `cd` a shell builtin rather than a program?
- Because it is too small to be worth a file
- Because a separate program could not change the shell's own working directory
- For historical reasons only
`cd notes.txt` answers `Not a directory`. Why?
- The file is empty
- You can only enter directories, and `notes.txt` is a file
- You need to be root
Practice
Move into the directory /root/lab/l5/docs.
Go to /root/lab/l5/docs/2024 with `cd`, and once you are there create an empty file called `visited` using just its name (`touch visited`).
From wherever you are, go back to your home directory with the shortest possible command.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.