Navigating the File System — Terminal

The file system is the arrangement of all files and folders, like a tree with one root: /, the top folder. Your home folder (your personal folder), for…

The terminal has no folder window you can click. You always have to know which folder you are in, and you check it by typing. You also see what is inside a folder and move to another folder by typing. To move, you name where you want to go, like when you give someone directions to a friend's house. The full address, "12 Park Road, Leeds, England", is right wherever the person starts from. Directions, "from here turn left, second house", are only right if the person is standing where you are. In the terminal, the address of a file or folder is called a path. A path like the full address is called an absolute path, and a path like the directions is called a relative path.

Folders inside folders: the file system tree

On Linux and macOS, every file and folder is arranged like an upside-down tree with one root at the very top. This arrangement is called the file system. This is part of the tree on Budi's laptop. Lines join each folder to what is inside it, for example budi inside home:

/
├── etc
├── home
│   └── budi
│       ├── Documents
│       ├── Downloads
│       └── web-project
│           ├── logs
│           └── src
├── tmp
└── var
    └── log

pwd: where are you?

The folder you are in right now is called the working directory. pwd (print working directory) prints it as a full path, starting from the root:

budi@laptop:~$ pwd
/home/budi

The prompt shows that folder too, in short form: ~ in the prompt is the same as /home/budi.

ls: seeing what's in a folder

You know ls from the previous lesson. Here are three ways to use it on Budi's laptop:

budi@laptop:~$ ls
Documents  Downloads  web-project
budi@laptop:~$ ls web-project
error.log  index.html  logs  script.js  src  style.css
budi@laptop:~$ ls -a web-project
.  ..  .env  error.log  index.html  logs  script.js  src  style.css

Here are -l and -a combined into -la:

budi@laptop:~$ ls -la web-project
total 36
drwxrwxr-x 4 budi budi 4096 Oct  7 21:14 .
drwxr-x--- 5 budi budi 4096 Oct  6 19:30 ..
-rw-rw-r-- 1 budi budi   34 Oct  6 19:32 .env
-rw-rw-r-- 1 budi budi   84 Oct  7 21:30 error.log
-rw-rw-r-- 1 budi budi  196 Oct  7 20:41 index.html
drwxrwxr-x 2 budi budi 4096 Oct  6 19:46 logs
-rw-rw-r-- 1 budi budi   69 Oct  7 20:55 script.js
drwxrwxr-x 2 budi budi 4096 Oct  6 19:45 src
-rw-rw-r-- 1 budi budi   59 Oct  7 20:48 style.css

cd: moving to another folder

cd (change directory) moves you to another folder. When it works, cd shows nothing. Only the prompt changes:

budi@laptop:~$ cd web-project
budi@laptop:~/web-project$ cd src
budi@laptop:~/web-project/src$ pwd
/home/budi/web-project/src
budi@laptop:~/web-project/src$ cd ..
budi@laptop:~/web-project$ cd ~
budi@laptop:~$ cd -
/home/budi/web-project
budi@laptop:~/web-project$

Absolute paths and relative paths

Say you are in /home/budi/web-project/src. Before you look at the table, guess: which folder does cd ../logs take you to? This is what happens:

You type Counted from You end up in
cd .. /home/budi/web-project/src /home/budi/web-project
cd ../logs /home/budi/web-project/src /home/budi/web-project/logs
cd /var/log / (root) /var/log
cd logs /home/budi/web-project/src error: bash: cd: logs: No such file or directory

cd ../logs first goes up to web-project, then into logs, so you end up in /home/budi/web-project/logs. The last row fails because there is no logs folder inside src.

A path that starts with ~, such as ~/web-project, also leads to the same place from anywhere. The reason: the shell replaces ~ with /home/budi before the command runs.

From commands to code

In the exercise, you write two JavaScript functions (named pieces of code) that follow the rules above. The first function turns a task text into a command, as in this table:

Task Command
list contents ls -la
where am i pwd
go up cd ..
go home cd ~
enter folder src cd src

⚠️ What gets you lost between folders

TL;DR: The file system is the arrangement of all files and folders, like a tree with one root: /, the top folder. Your home folder (your personal folder), for example /home/budi, is shortened to ~. pwd prints the folder you are in, and ls shows what is in it (-a includes hidden files, -l shows details, -h makes sizes easy to read). cd moves you: cd name goes into a folder, cd .. goes up one level to the parent folder, cd ~ goes home, cd - goes back to the folder you were in before the last cd. A path is the address of a file or folder. An absolute path starts with / and leads to the same place from anywhere. A relative path, such as ./src or src/index.js, counts from the folder you are in.

⚡ Where are you?

Write:

pwd

Meaning: pwd prints the folder you are in as a full path (the folder's address), starting from the root / (the top folder), like /home/budi. When you're not sure where you are, run pwd first.

⚡ See what's inside

Write:

ls -la web-project

Meaning: This command shows what is in the folder web-project. -a also shows hidden files (files whose name starts with a dot), like .env. -l shows one line per item, with details. When the first letter of a line is d, that item is a folder.

⚡ Move between folders

Write:

cd web-project
cd ..
cd ~

Meaning: cd followed by a folder name (cd name) goes into that folder, here web-project. cd .. goes up one level to the parent folder (the folder that holds this one). Don't forget the space between cd and ... cd ~ goes home to your home folder.

⚡ Absolute or relative

Guess first: you're in /home/budi/web-project/src. Where does cd ../logs take you?

To /home/budi/web-project/logs. .. goes up to web-project first, then cd goes into logs inside it.

Meaning: An absolute path starts with / and leads to the same place from anywhere. A relative path counts from the folder you are in. Only the first character decides which kind a path is, so src/index.js is still relative.

What you will learn