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
/is the root, the very top. There is noC:orD:like on Windows: everything lives under/.- Directory is another word for folder.
/home/budiis Budi's home folder, where his own files live. To read that path: start at the root/, go into the folderhome, then into the folderbudi. The shell shortens it to~. On macOS, the home folder is at/Users/budi.- If you use WSL on Windows, the
C:drive appears as the folder/mnt/c. SoC:\Users\budibecomes/mnt/c/Users/budi. In Git Bash, the same path is written/c/Users/budi. Keep your projects in your Linux home folder (~), not in/mnt/c. Microsoft recommends this, because Linux commands read files in the Linux home folder faster than files in/mnt/c.
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
lswith no argument shows what is in the folder you are in. With an argument,lsshows what is in the folder you name.-a(all) also shows names that start with a dot. Files like.envare hidden on purpose, by giving them a name that starts with a dot..is this folder itself, and..is its parent folder (the folder that holds this one).-l(long) shows one line per item, with details.
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
- The first letter of each line:
dmeans directory,-means a plain file. - The number before the date is the size in bytes (a unit of file size):
index.htmlis 196 bytes. In this example, every folder is listed as 4096. - After the size comes the time the item last changed, then its name. The
rwxletters andbudi budiare covered in the Permission lesson. -hmakes sizes easy to read (4.0K, about 4 thousand bytes). That is whyls -lahis a favourite combination of many developers.
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$
cd namegoes into a folder inside the folder you are in. Replacenamewith the name of that folder.cd ..goes up one level to the parent folder. There is a space betweencdand...cd ~, or justcd, goes home to your home folder.cd -goes back to the folder you were in before your lastcd, then prints its path. This is useful for going back and forth between two folders.
Absolute paths and relative paths
- An absolute path always starts with
/and counts from the root:/home/budi/web-project/src. It leads to the same place whichever folder you type it from, like a full address. - A relative path doesn't start with
/and counts from the folder you are in:src,./src(.means the folder you are in),../logs,src/main.js. Where it leads depends on where you are, like directions.
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 |
- For an
enter folder ...task, the folder name is taken from the task text, soenter folder testsbecomescd tests. Check the start of the task text (task) withtask.startsWith("enter folder "), then join"cd "with the rest of the text. - The second function recognises an absolute path by its first character:
path.startsWith("/"). A slash in the middle changes nothing, sosrc/index.jsis still relative.
⚠️ What gets you lost between folders
cd..with no space. Bash reads it as one command name and answersbash: cd..: command not found. Writecd ...- Forgetting where you are. A relative path that works in one folder can fail in another. When in doubt, run
pwdfirst. - Upper and lower case. On Linux,
cd documentsfails because the folder is calledDocuments. On macOS, the same command usually still goes intoDocuments, because the default disk format of macOS (APFS) treats upper and lower case as the same. - Thinking hidden files aren't there.
.envdoesn't show in a plainls. Usels -aorls -la. - In the exercise, treating
src/index.jsas absolute because it has a/. Only the first character decides.
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.