How a path leads to a file
A file system turns a path such as /home/student/notes.txt into the disk blocks that hold the file. It uses directories and records called inodes. Knowing these steps explains links, why renaming a huge file is instant and why a disk can run out of room with space still free.
This lesson follows one path, a step at a time.
By the end of this lesson
Note
I can explain how a path is turned into a file.
Key words
| Word | What it means | Example |
|---|---|---|
| Inode | the record that holds a file's details and where its blocks are | |
| Directory | a file that lists names and the inode for each | |
| Block | a fixed-size piece of disk | 4096 bytes |
| Path | the list of names that leads to a file | /home/student/notes.txt |
| Hard link | a second name for the same inode | |
| Symbolic link | a small file that holds the path of another |
The cells students fill in are left blank in this preview.
An inode holds details, not the name
A disk is a long row of blocks. For each file the system keeps one record, the inode. It holds the type, the owner, the permissions, the size, the times, the number of names and the list of blocks that hold the data.
The inode has a number, unique on that disk. It does not hold the file's name.
Print the type of a file, a folder
stat -c %F /etc/passwd /Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: one inode
| Field | Example | Meaning |
|---|---|---|
| Inode number | 14 | The record's number on this disk |
| Type | regular file | A file, a directory or a link |
| Owner and mode | student, rw-r--r-- | Who may read, write or run it |
| Size | 6 bytes | How much data it holds |
| Link count | 1 | How many names point at it |
| Block list | block 880 | Where the data sits on the disk |
The cells students fill in are left blank in this preview.
A directory lists names and inodes
A directory is a file whose data is a list of pairs: a name and an inode number. That list is where names live.
To open /home/student/notes.txt, the system starts at the root directory, whose inode number is always known. It looks up home and gets an inode. That is a directory too, so it reads it and looks up student, and so on to the last name.
Worked example: resolving /home/student/notes.txt
| Step | Directory read | Name looked up | Inode found |
|---|---|---|---|
| 1 | root (inode 2) | home | 5 |
| 2 | inode 5 | student | 9 |
| 3 | inode 9 | notes.txt | 14 |
| Result | inode 14: the file |
The cells students fill in are left blank in this preview.
Two names can point at one inode
Because a name is only a directory entry, a second entry can point at the same inode. That is a hard link. The inode counts its names, and the data goes only when the count reaches 0.
A symbolic link is different. It is a small file of its own that holds a path. Make three small files in the scratch folder, which the lab says is safe to delete from.
Make a file of six bytes
printf 'hello\n' > ~/scratch/demo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Print its size and its number of names
stat -c '%s %h' ~/scratch/demo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Give it a second name
ln ~/scratch/demo.txt ~/scratch/demo-link.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Print the number of names again
stat -c %h ~/scratch/demo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Check that both names have one inode number
[ "$(stat -c %i ~/scratch/demo.txt)" = "$(stat -c %i ~/scratch/demo-link.txt)" ] && echo sameShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a symbolic link, print its type
ln -s ~/scratch/demo.txt ~/scratch/demo-sym.txt && stat -c %F ~/scratch/demo-sym.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Remove the three files
rm ~/scratch/demo.txt ~/scratch/demo-link.txt ~/scratch/demo-sym.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think a file's name is part of the file. The name lives in a directory, and the data lives under the inode. That is why a rename changes only a directory, and why one file can have two names. A symbolic link is the exception: it breaks if the path it holds stops existing.