Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Operating Systems

    Files and File Systems

    The disk's numbered pages are blocks (commonly 4 KB).

    Chapter 10 of 126 min read

    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.md
    • Absolute 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:

    Output
      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                    safe

    Permissions (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

    • write returning success ≠ data is on disk. Only fsync (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.txt and notes.txt as 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 questions
    1

    What does a file system do?

    Show answer

    Organises raw disk blocks into named files and directories, tracking where each file's data is.

    2

    What's an inode?

    Show answer

    The record holding a file's details (size, owner, permissions, data block locations); the name lives in the directory.

    3

    What are file descriptors 0, 1 and 2?

    Show answer

    stdin, stdout, stderr.

    4

    Your app calls write and gets "success", then the power fails. Is the data safe? Why?

    Show answer

    Not necessarily — it may still be only in the page cache in RAM. Only fsync guarantees it's on disk.

    5

    Absolute vs relative path?

    Show answer

    Absolute starts from the root /; relative starts from the current directory.