pwd, ls and cd — Finding Your Way
You open a terminal and see only a blinking cursor. There are no windows or icons, so you can't glance at a desktop to see where you are. On the command line you ask, and three tiny commands do most of the asking. Throughout this course we'll follow Asha, who keeps her community garden's records in a folder called garden in her home directory.
The rule
Where am I, what is here, go there: pwd, ls, cd.
pwd (print working directory) tells you which folder the shell is "standing in" right now:
$ pwd
/home/ashals (list) shows what is inside the current folder, or inside any folder you name:
$ ls
garden photos todo.txt
$ ls garden
harvest.csv notes.txt plotsTwo options you'll use every day:
ls -lgives the long listing, with permissions, owner, size and date on each line.ls -ashows all entries, including hidden ones whose names start with a dot, such as.bashrc.
You can combine them: ls -la.
cd (change directory) moves you:
$ cd garden
$ pwd
/home/asha/gardenA path is written one of two ways:
- Absolute paths start with
/and describe the full route from the top of the system:/home/asha/garden. - Relative paths start from where you are now:
garden, orplots.
A few special names work anywhere:
| Name | Means |
|---|---|
. |
this directory |
.. |
the parent directory, one level up |
~ |
your home directory |
- |
(with cd) the directory you were in before |
A plain cd with nothing after it takes you home.
The shortcut
Before every cd, read the path's first character. A / means "start from the top". A ~ means "start from home". Anything else means "start from here", so you need to know where "here" is, which is what pwd tells you.
Where am I?
pwdprints/home/asha/garden/plots.
The common mistake is typing a relative path from the wrong place. cd plots only works if plots is directly inside your current directory. When a cd fails with "No such file or directory", run pwd and ls before you try again.
Drills
Answer fast — trust the rule.
Asha is in /home/asha/garden. Which command prints that path?
Which command shows hidden files such as .bashrc as well as the normal ones?
Asha is in /home/asha/garden/plots. Where does cd .. take her?
Which of these paths means the same place no matter where you run cd?
Asha runs cd /etc, then cd ~/garden, then cd -. Where is she now?
Remember
- pwd says where you are, ls shows what's there, cd moves you.
- Read the first character of a path:
/starts at the top,~starts at home, anything else starts from here. - If a
cdfails, check where you are withpwdandlsbefore guessing again.
