How does a file system work
In the last lesson, drivers let us talk to devices like the disk. But open up a disk, and there are no files or folders, just numbered cells lined up endlessly. In this flat sea of cells, how do you find a single photo of yours?
The disk's bare face: just numbered cells
On screen, we see files as icons and open folders to find more folders inside. But the disk itself has none of that.
A disk is really just a huge storage cabinet of identical cells laid out flat in numbered order: 0, 1, 2, and so on. Each cell is called a block.
Blocks hold pieces of data, but the disk itself has no idea which cell belongs to which file or what anything is called. Numbers and data: that's all there is.
Press a cell to put data in it. The cells have only numbers, no names and no folders. This is the disk's bare face.
The cells are told apart only by number, and their contents have no connection to each other. A big file won't fit in a single cell, and empty cells are scattered here and there.
So one file has to be split across several cells, but how do you gather the scattered pieces back together later?
This is the first problem the operating system has to solve.
A file: a name tag and map that tie scattered blocks together
The solution is surprisingly simple. The operating system keeps a small note: "The file named photo.jpg is in cells 3, 7, and 12, in that order." This note is the map.
Give it a name, and the operating system opens the map, visits the scattered cells in order, and gathers the pieces.
So a file is really two things put together: the pieces of data scattered across cells, and the map that ties them together under a name. What we double-click is this map; the disk just reads the cells the map points to.
Press the name tag, and following the map, the scattered cells (3,7,12) come together in order to form a single file.
One map tied the scattered cells into a single file. Thanks to it, we can call up a file by name without knowing any cell numbers.
But what if there are thousands of files instead of one or two? Thousands of name tags pile up, and it's overwhelming again.
We need a way to organize all these name tags. Now it's the folder's turn.
A folder: one more file that holds files
A folder sounds like something completely different from a file, but in fact a folder is just one more file. Only its contents are special.
An ordinary file holds photos or text, but a folder is a file that holds "a list of the name tags of the files inside it." So opening a folder means reading that list to see which name tags are in it.
You can put folders inside folders, too. Then a shape like a branching tree appears. Start at the root at the top and climb down the branches, and you reach the file you want.
Press a folder to open it and see the files and subfolders inside. You can follow the branches from the root down to the file you want.
On top of the flat sea of cells, a neat tree branching up and down now stands. Because a folder is just another file, the operating system built this structure from the same idea of a file, without inventing a new mechanism.
But one question remains: when we delete a file made this way, does its data really vanish from the disk right away?
Let's peek at that secret in the next step.
Deleting means erasing only the map
Intuitively, deleting a file seems like it should wipe its data off the disk. But the operating system usually doesn't do that, because clearing every cell that holds data takes a long time.
To finish quickly, it erases only the name tag and map and leaves the data in the cells as it is. Instead, it just marks those cells as "now empty, free to reuse."
With the map gone, the road to that data is cut, so to us the file seems to have disappeared. But the contents of the cells are still sitting where they were.
Delete a file: the name tag and map fade (the road is cut), but the data in the cells stays in place. It only disappears when a new file overwrites those cells.
That's why recovery tools can sometimes bring a deleted file back: find the map again, and the cells still sitting in place can be stitched back together. On the other hand, once a new file overwrites those cells, the original data is gone for good. Today's solid-state drives often clear freed cells soon afterward, which makes recovery harder.
In the end, the heart of deleting wasn't erasing the data but erasing the map to the data. Files, folders, and deletion all stand on one idea: maps and name tags.
A tree-shaped illusion laid over a flat disk
Let's step back and look at the whole. The disk's true form was flat, numbered cells. On top of it, the operating system worked three kinds of magic.
First, it tied scattered cells into a single file with name tags and maps. Second, it made folders, files that hold other files, and raised a tree. Third, when deleting, it erased the map rather than the data.
Thanks to these three, we can handle files comfortably with just names and folders, without ever knowing a single cell number.
Press the three cards in order to see at a glance how the illusion builds up: from flat blocks, to files tied together by name tags, to a folder tree.
Just as virtual memory showed a memory bigger than the real one, a file system is an illusion that shows flat cells as a neatly organized tree. The reality is simple; the convenience is what the operating system adds on top.
But if several people share this neat tree on one computer, is it all right for anyone to open and delete other people's files? In the next lesson, we'll talk about permissions, which keep order over who can read, write, and run what.