Terminal survival and the directory tree¶
Practical 4.1 Where is my file? PDF
Think of the last photo you took. Where is it right now? You point at your phone. Fine — but where inside the phone? Could you find it with a microscope?
Nobody can answer that on the first try. This unit answers it. And to walk down to where files really live, you need a tool that every server, every developer and every system administrator uses daily: the terminal. Ten short words, and you can do what takes forty clicks.
What you will learn
- Follow one file down the chain: file → directory → file system → partition → disk
- Read the prompt and say who you are, on which machine and where
- Move around with
pwd,lsandcd, using absolute and relative paths,..,~and the Tab key - Read, create, copy, move and delete files with
cat,less,mkdir,touch,cp,mv,rm - Say what lives in
/home,/etc,/usr,/var,/tmp,/boot,/devand/proc, and its Windows cousin - Why
rmis not "move to the recycle bin" — and why/rootis not the top of the tree
Where does a file live?¶
When you double-click an icon, the computer walks down five layers. Each one sits inside the next:
| Layer | What it is |
|---|---|
| File | A row of bytes with a name |
| Directory (folder) | A list of names: files and other directories |
| File system | The rules and the index that say which blocks of the disk belong to which file |
| Partition | A piece of a disk, with a start and an end |
| Disk | The device that keeps the bits without power (the HDD or SSD from Storage) |
A file is cut into blocks, usually of 4 KiB (4,096 bytes) each, and the file system writes down which blocks hold it. You can see this on deb-lab. Save a file hello.txt containing the word hello in Documents, then ask:
-rw-rw-r-- 1 student student 6 ... hello.txt
4.0K /home/student/Documents/hello.txt
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 ... ... ... ... /
ls -lsays the file is 6 bytes: five letters plus the end-of-line byte (0A, from Binary).dusays it takes 4.0K on the disk. The file system hands out space in whole blocks, so 6 bytes cost one full block.dfnames the partition that holds it:/dev/vda1(orvda2, depending on your install). The commandlsblkthen shows the diskvda(25G) with that partition hanging under it.
And the disk vda? On the host, virt-manager shows that the whole disk of deb-lab is one file, ending in .qcow2, usually in /var/lib/libvirt/images/. A box inside a box: your file lives in a file system, inside a disk that is itself a file on the host's real SSD. That is why in this unit you can break disks and nobody cries — the new disks you add will be just files.
Careful: the file is not “in” the folder
A directory is only a list of names. The bytes sit in blocks somewhere on the partition, and the file system knows where. You will open that index in File systems.
Why a terminal?¶
1969, Bell Labs: Ken Thompson and Dennis Ritchie start Unix. There are no screens. You sit at a teletypewriter, a typewriter wired to the computer: you type a line, the machine prints the answer on paper. That is why Linux still calls a terminal a tty, and why commands are so short — every letter was noise and paper.
The shell is the program that reads the line you type and runs it.
Ours is Bash, written by Brian Fox for the GNU project in 1989. The name is a joke: Bourne-Again SHell, after the older shell of Stephen Bourne.
It is 2026. Why would anyone still type?
| Reason | Why it matters |
|---|---|
| Servers have no screen | ubu-srv has no desktop at all. Most of the Internet runs on machines like it. |
| Exact | "Click the third icon" changes with every version. ls /etc has meant the same for fifty years. |
| Repeatable | A line can be saved and run again tomorrow, or on 200 machines (shell scripts, later in this unit). |
| Fast | Once you know ten words. |
The prompt¶
Open the Terminal in deb-lab. The shell greets you with the prompt:
| Piece | Meaning |
|---|---|
student |
Who I am |
deb-lab |
Which machine |
~ |
Where I am: ~ is short for my home directory |
$ |
I am a normal user (# would mean the administrator, called root) |
The shell is waiting. Nothing happens until you press Enter.
No message is good news
The terminal only answers when there is something to say. A command that prints nothing usually worked.
Where am I? pwd, ls, cd¶
student@deb-lab:~$ pwd
/home/student
student@deb-lab:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
student@deb-lab:~$ ls -a
. .. .bash_logout .bashrc .profile Desktop Documents ...
pwd= print working directory: the answer to "where am I?".ls= list.ls -a= all: names that start with a dot are hidden.ls -l= long: one line per entry. The first letter says what it is:ddirectory,-file,llink.
Now move. cd = change directory:
student@deb-lab:~$ cd /etc
student@deb-lab:/etc$ cd ..
student@deb-lab:/$ ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
student@deb-lab:/$ cd home/student
student@deb-lab:~$ cd Documents
student@deb-lab:~/Documents$ cd
student@deb-lab:~$
Watch the prompt: the part before $ changes every time you move.
Paths: the address of a file¶
A path is the address of a file or directory in the tree.
Think of a postal address — the one analogy of this page. There are two ways to give one:
| Absolute path | Relative path | |
|---|---|---|
| Starts with | / (the root) |
a name, . or .. |
| Like | "Carrer del Sindicat 5, Palma, Mallorca": works from anywhere | "the second door on the left": depends on where you stand |
| Example | cd /home/student/Documents |
cd Documents (only works from ~) |
The short forms you will use every day:
| Symbol | Means |
|---|---|
/ |
The root: the top of the tree |
. |
Here |
.. |
One level up: the parent directory |
~ |
My home |
cd alone |
Go home |
Three keys that make typing bearable¶
- Tab completes names. Type
cd /usr/shand press Tab: the line becomescd /usr/share/. Press it twice when there are several choices. If Tab does nothing, the name does not exist — a free spell-check. - Up brings back the previous line. Nobody types a long command twice.
- Ctrl+C cancels the current line or stops a running command. Ctrl+L clears the screen.
“Linux does not find my folder”
Three usual causes, in this order:
- Capital letters. Names are case-sensitive:
documentsis notDocuments. - A missing or extra space.
- A relative path typed from the wrong place.
Run pwd and ls before asking anyone.
How to read a command¶
Every command line has up to three parts, separated by spaces:
- The command is the program to run.
- Options change how it works and start with
-. They can be joined:ls -la. - Arguments say what it works on. With no argument, most commands use the current directory.
When a command answers with an error, the error is the answer. Read it:
student@deb-lab:~$ cd /nope
bash: cd: /nope: No such file or directory
student@deb-lab:~$ Ls
bash: Ls: command not found
student@deb-lab:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
| Message | It means |
|---|---|
No such file or directory |
The path is wrong: check spelling, capitals and where you are |
command not found |
The command name is wrong (Ls is not ls) or not installed |
Permission denied |
It exists, but your user may not touch it |
Make, copy, move, delete¶
| Command | Does | Example |
|---|---|---|
cat |
Prints a whole file | cat /etc/hostname → deb-lab |
less |
Shows a file page by page (Space next, q quit) |
less /etc/os-release |
mkdir |
Makes a directory | mkdir lab |
touch |
Makes an empty file (size 0) | touch notes.txt |
cp |
Copies | cp /etc/hostname . (the dot = "to here") |
mv |
Moves or renames: it is the same command | mv notes.txt todo.txt |
rm |
Deletes a file | rm hostname |
rm -r |
Deletes a directory and everything inside (recursive) | rm -r old |
rmdir |
Deletes a directory only if it is empty | rmdir lab |
A short session in your home, one line at a time:
student@deb-lab:~$ mkdir lab
student@deb-lab:~$ cd lab
student@deb-lab:~/lab$ touch notes.txt
student@deb-lab:~/lab$ mv notes.txt todo.txt
student@deb-lab:~/lab$ mkdir old
student@deb-lab:~/lab$ mv todo.txt old/
student@deb-lab:~/lab$ rm old
rm: cannot remove 'old': Is a directory
student@deb-lab:~/lab$ rm -r old
student@deb-lab:~/lab$ cd ..
student@deb-lab:~$ rmdir lab
There is no recycle bin here
rm does not ask and does not forgive. Gone is gone (until you learn backups, later in this unit). Before you press Enter on an rm, read the line again, slowly.
Careful: cp and mv do not create folders
mv todo.txt old when old does not exist simply renames the file to old. Check with ls after every move.
The tree: FHS¶
Type ls /. Who decided those names? They come from Unix, and since the 1990s they are written down in a standard: the Filesystem Hierarchy Standard (FHS). Version 3.0 was published by the Linux Foundation on 3 June 2015. That is why what you learn on Debian works on Ubuntu, Fedora or a web server in a data centre.
The big idea: Linux has one tree, and it starts at /. No C:, no D:. A USB stick or a second disk appears as a directory somewhere inside the tree — that is called mounting, and it has its own page.
flowchart TB
ROOT["<b>/</b><br>the root: top of the one tree"]
ROOT --> HOME["<b>/home</b><br>one folder per person"]
ROOT --> ETC["<b>/etc</b><br>system configuration"]
ROOT --> USR["<b>/usr</b><br>installed programs"]
ROOT --> VAR["<b>/var</b><br>changing data, logs"]
ROOT --> OTHER["<b>/tmp /boot /dev /proc</b><br>/root /media /mnt"]
HOME --> STUDENT["<b>/home/student</b><br>your home: ~"]
STUDENT --> DOCS["<b>Documents</b>"]
USR --> BIN["<b>/usr/bin</b><br>the commands"]
VAR --> LOG["<b>/var/log</b><br>the logs"]
| Directory | What lives there | Windows cousin |
|---|---|---|
/ |
The root: the top of the one tree | C:\ |
/home |
One directory per person: /home/marta. Your files and settings. |
C:\Users\marta |
/root |
The home of the administrator (root). Closed to everybody else. | The Administrator's profile |
/etc |
The configuration of the whole system, as text files | The Registry and C:\ProgramData |
/usr |
Installed programs; /usr/bin holds the commands. /bin is only a link to it. |
C:\Program Files, C:\Windows\System32 |
/var |
Data that changes while the system runs: logs in /var/log |
The event logs (Event Viewer) |
/tmp |
Temporary files. Anybody can write; keep nothing there. | C:\Windows\Temp |
/boot |
The kernel and the bootloader (see Boot) | The EFI System Partition |
/dev |
The devices, shown as files: the disk is /dev/vda |
Device Manager (no files) |
/proc |
Not on the disk at all: the kernel answers questions there, live | Task Manager |
/media, /mnt |
Where USB sticks and extra disks are attached | D:, E:... |
Careful: three things called root
/ is the root directory, the top of the tree. /root is the home of the user called root. And root is also that user, the administrator. Context tells which.
The Windows tree in three lines¶
- Windows has one tree per drive, each with a letter:
C:,D:. - The separator is the backslash:
C:\Users\marta\Documents. - Names are not case-sensitive:
Notes.txtandnotes.txtare the same file there, and two different files on Linux.
Practical 4.1 · Treasure hunt¶
It is your first day at Tramuntana Apps, a 12-person app studio in Palma. Before you touch anything, get to know the machine. The Practical 4.1 sheet (the button at the top) has ten questions to answer with today's commands only — the Debian version in /etc, the kernel in /boot, the processors in /proc/cpuinfo — and then a small tree to build for the three departments, dev, design and admin. You hand it in on Classroom as practical-4.1-surname.md.
Key takeaways¶
- A file lives in a chain: file → directory → file system → partition → disk. Space is given in whole blocks (4 KiB), and a VM's whole disk is one
.qcow2file on the host. - The shell (Bash) reads your line; the prompt says who, where and which machine. A command line is command + options + arguments.
pwd,ls,cdmove you around. An absolute path starts at/and works from anywhere; a relative path depends on where you stand...is the parent,~is home.cat,less,mkdir,touch,cp,mv,rm.mvalso renames;rmhas no recycle bin.- Linux has one tree described by the FHS:
/homepeople,/etcconfiguration,/usrprograms,/varlogs,/devdevices,/procthe live kernel. Windows has one tree per drive letter.
Quick check¶
1. You are in /home/marta/Documents and type cd ... Where are you now?
-
/home/marta/Documents -
/home/marta -
/home -
/
.. is the parent directory: one level up. To go back to wherever you were before, the command would be cd -, not cd ...
2. Which one is an absolute path?
-
Documents/notes.txt -
../notes.txt -
notes.txt -
/home/marta/notes.txt
An absolute path starts with /, the root, so it works from anywhere — like a full postal address. The others are relative: they depend on where you stand.
3. Where do the configuration files of the whole system live?
-
/etc -
/home -
/tmp -
/boot
/etc holds the system configuration as text files. /home holds people's files, /tmp temporary files and /boot the kernel and bootloader.
4. What does rm -r dev do?
- Moves
devto the recycle bin - Deletes
devonly if it is empty - Deletes
devand everything inside, for good - Renames
devtor
-r means recursive: the directory and all its contents. There is no recycle bin in the terminal. Deleting only an empty directory is the job of rmdir.
5. What is the Windows cousin of /home/marta?
-
C:\Program Files\marta -
C:\Users\marta -
C:\Windows\marta -
D:\marta
Each person's files and settings live in C:\Users\<name> on Windows and in /home/<name> on Linux. C:\Program Files is closer to /usr.
6. A file holds 6 bytes, on a file system with 4 KiB blocks. How much disk does it take?
- 6 bytes
- 4 KiB
- 8 KiB
- Nothing until it grows
The file system hands out space in whole blocks. One block of 4,096 bytes for 6 bytes of data: that is why du says 4.0K while ls -l says 6.
7. Seen from the host, what is the whole disk of the VM deb-lab?
- One
.qcow2file - One partition of the real SSD
- A directory full of small files
- A second physical SSD
virt-manager stores each virtual disk as one file on the host's file system. Inside it there is a partition table, partitions and file systems — a box inside a box.
8. A colleague types cd documents and gets No such file or directory, but ls shows Documents. Select the two that are true.
- Linux names are case-sensitive, so
documentsis a different name - Typing
cd Docand pressing Tab would have written the right name - The folder is hidden because it starts with a capital letter
- The command needs
sudoto enter a folder in the home directory
On Linux, Documents and documents are two different names. Tab completion only completes names that exist, so it fixes the capital for you. Hidden names start with a dot, not a capital.
Now you can move around the tree. But what if the file you need is one among thousands? Next: Finding information.