The Plain Picture#
A disk, underneath, is like a giant notebook with millions of numbered blank pages and nothing else. No titles, no chapters. If you just scribbled notes on random pages, you'd never find anything again.
So you reserve the first few pages for an index: "Note Biology is on pages 402, 403 and 9,120. Folder Semester 1 contains Biology and Chemistry." Now you can find, grow and delete notes.
The Real Names#
The disk's numbered pages are blocks (commonly 4 KB).
The system that organises blocks into named files and folders is a file system. Common ones: ext4 and XFS (Linux servers), APFS (your Mac), NTFS (Windows).
Folders are called directories. The folder tree starts at the root, written
/.On Unix-like systems, each file's details — size, owner, permissions, which blocks hold its data — are stored in a record called an inode. The file's name lives in its directory, pointing to the inode.
Under the Hood#
Paths#
A path is a file's address in the folder tree:
/ ← root
└── Users/
└── priya/
└── Documents/
└── 01_STUDY/
└── README.md ← path: /Users/priya/Documents/01_STUDY/README.mdAbsolute path — starts from
/:/Users/priya/Documents/01_STUDY/README.md.Relative path — starts from where you are now:
../notes/README.md(..means "one folder up").
File descriptors#
When a process opens a file, the kernel returns a small number — a file descriptor (the 3 in
Ch 2's example). The process uses that number for every later read/write. Every process starts with
three already open:
0 stdin standard input (keyboard, by default)
1 stdout standard output (the terminal screen)
2 stderr standard error (also the screen, but a separate channel for errors)Network connections also get file descriptors — to Unix, "everything is a file": files, keyboards,
network connections and pipes are all read and written through the same system calls. There's a limit
on how many a process may have open (on this Mac, ulimit -n shows the current limit). Busy servers hit
"Too many open files" when they leak connections.
Writes are not on disk yet — the page cache and fsync#
When you call write, the kernel usually copies the bytes into a RAM area called the page cache and
returns immediately. The data reaches the disk later (typically within seconds). This makes writes fast,
and it's also where spare "free" RAM goes (Ch 5 gotcha).
But if the power fails before that happens, the data is lost — even though write said "success".
To force the data onto the disk, a program calls fsync, which waits until the disk confirms.
It's slow (milliseconds), which is why databases think carefully about when to call it. This is the
exact problem databases solve with a write-ahead log (Storage Engines (coming soon)).
app write() ──▶ page cache (RAM) ──── later, or on fsync() ────▶ SSD
fast, but lost on power failure safePermissions (preview)#
Every file records an owner and who may read, write or execute it. You'll use this daily
on servers — chmod, chown, and the famous rwxr-xr-x — in The Linux Command Line.
Gotchas
writereturning success ≠ data is on disk. Onlyfsync(or closing with the right options) gives that guarantee.Deleting a file that a process still has open doesn't free the space until the process closes it. A classic "disk is full but I deleted the huge log file" mystery on servers.
Case sensitivity: Linux file systems treat
Notes.txtandnotes.txtas different files; macOS (by default) treats them as the same. Code that works on your Mac can break on a Linux server.
Check yourself
5 questionsWhat does a file system do?
Show answerHide answer
Organises raw disk blocks into named files and directories, tracking where each file's data is.
What's an inode?
Show answerHide answer
The record holding a file's details (size, owner, permissions, data block locations); the name lives in the directory.
What are file descriptors 0, 1 and 2?
Show answerHide answer
stdin, stdout, stderr.
Your app calls write and gets "success", then the power fails. Is the data safe? Why?
Show answerHide answer
Not necessarily — it may still be only in the page cache in RAM. Only fsync guarantees it's on disk.
Absolute vs relative path?
Show answerHide answer
Absolute starts from the root /; relative starts from the current directory.