Kodokon kodokon.com

Moving around: pwd, cd and ls

Find your bearings in the file tree and travel from folder to folder with pwd, ls and cd.

7 min · 3 questions

Open this lesson in Kodokon

In a file explorer you can always see where you are: the path shows at the top of the window and the folder contents are spread out in front of you. In a terminal, none of that is visible by default. And yet you are always somewhere: at every moment the shell sits in one specific folder, called the current folder or working directory. Three commands are enough to find your way: one to know where you are, one to see what is there, one to move. The first is pwd, for print working directory. It changes nothing, it simply answers the question "where am I?", and it is the reflex to have the moment you feel lost.

BASH
yoann@laptop:~$ pwd
/home/yoann
pwd replies with the full path of the current folder.

The second is ls, for list: it shows the contents of the current folder. On its own it gives a compact list of names. With the option -l (for long), it details each line: permissions, owner, size and modification date. With the option -a (for all), it also shows hidden files, meaning the ones whose name starts with a dot. An option goes after the command, preceded by a dash, and options can be combined: ls -la.

BASH
yoann@laptop:~$ ls
Desktop  Documents  Downloads  Pictures

yoann@laptop:~$ ls -l
total 16
drwxr-xr-x 2 yoann yoann 4096 Jul 20 09:14 Desktop
drwxr-xr-x 5 yoann yoann 4096 Jul 21 18:02 Documents
drwxr-xr-x 3 yoann yoann 4096 Jul 19 11:47 Pictures

yoann@laptop:~$ ls -a
.  ..  .bashrc  Desktop  Documents  Downloads  Pictures
The same command with and without options: ls, then ls -l and ls -a.

The third is cd, for change directory: it moves you. You give it the name of the folder to go to. Four shortcuts are worth learning by heart: cd .. goes up one level to the parent folder, cd ~ takes you straight back to your home folder, cd / leads to the root of the machine, and cd on its own does the same as cd ~. Note that a successful cd prints no message at all: in a terminal, silence means everything went fine.

BASH
yoann@laptop:~$ cd Documents
yoann@laptop:~/Documents$ pwd
/home/yoann/Documents
yoann@laptop:~/Documents$ cd ..
yoann@laptop:~$ cd ~
yoann@laptop:~$ pwd
/home/yoann
Going down into a folder, back up to the parent, then straight home.

Knowledge check

Make sure you remember the key points of this lesson.

  1. You have lost track of which folder you are in. Which command do you type?
    • ls
    • cd
    • pwd
    • whoami
  2. What does the command cd .. do?
    • It goes up to the parent folder
    • It goes back to the home folder
    • It shows hidden files
    • It creates a folder named ..
  3. You type cd Documents and the terminal prints nothing at all. What happened?
    • The command failed silently
    • The move worked: no message means no problem
    • The terminal is stuck, you have to close it