Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 55 ยท Operating Systems

File Systems

Learn how file systems organize data on disk: files, directories, paths, metadata, permissions, inodes and blocks, NTFS, ext4, APFS, and deleting vs erasing.

A storage device, whether a hard disk or an SSD, is just a long row of numbered blocks that each hold some bytes. It has no idea what a photo, a folder or a file name is. The file system is the layer of the operating system that turns those raw blocks into the files and folders you know. It decides where the bytes of each file go, remembers which blocks are free, and keeps track of names, sizes and permissions.

This lesson covers what a file is, how directories and paths work, the common file system types, what happens inside when you save or delete a file, and why "deleted" does not mean "gone". It follows Memory Management and relies on the devices from Storage: HDD, SSD and Beyond.

Files, directories and paths

A file is a named collection of bytes. To the file system, the contents are opaque: a photo and a spreadsheet are both bytes. What they mean is decided by the programs that open them (more about that in Compression and File Formats).

A directory (folder) is a special file that lists other files and directories. Because directories can contain directories, everything forms a tree with a single root at the top.

flowchart TD
  R["/ (root)"] --> H["home"]
  R --> E["etc"]
  H --> A["amira"]
  A --> D["documents"]
  A --> P["photos"]
  D --> N["notes.txt"]
  P --> B["beach.jpg"]

A directory tree. The path to notes.txt is /home/amira/documents/notes.txt.

A path is the route from some starting point to a file. An absolute path begins at the root (/home/amira/documents/notes.txt on Linux and macOS, or C:\Users\Amira\Documents\notes.txt on Windows). A relative path starts from the current directory, so from /home/amira, documents/notes.txt reaches the same file. Two special names help: . means "this directory" and .. means "the parent directory". Unix-style systems separate with /; Windows uses \ and starts with a drive letter, though most software accepts / as well.

Metadata and permissions

Besides its content, each file has metadata: its size, creation and modification times, its owner, its type, and its permissions. Permissions say who may read, write or execute the file. A common model has three classes of user (the owner, the owner's group, everyone else) and three rights (read r, write w, execute x), shown as nine letters such as rwxr-xr--. That string means the owner can do everything, the group can read and execute, and others can only read. Windows uses richer access-control lists but the idea is the same: the file system checks the rights on every access, which is how one user's files stay private from another's, and how a program can be stopped from changing system files.

The stand-in below builds a small in-memory file system with directories, files, paths and permissions. It is a model: the real thing stores this on disk, but the logic of walking a path is the same.

JS An in-memory file system with paths and permissions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common file system types

Different file systems make different trade-offs. You usually pick one when you format a drive; the main ones are:

File system Typically used by Notes
FAT32 Old USB sticks, memory cards, some cameras Works almost everywhere, but a single file cannot exceed 4 GiB
exFAT Large USB drives and SD cards Works across Windows and macOS, no 4 GiB limit, no permissions
NTFS Windows system drives Permissions, compression, encryption and a journal for crash recovery
ext4 Most Linux systems Journaling, large files and volumes, widely tested
APFS macOS, iPhones and iPads Designed for flash storage, with snapshots and fast copies

A journal is a log: before changing structures on disk, the file system writes down what it intends to do. If the power fails halfway, the journal lets it finish or undo the change on the next start instead of leaving a corrupted disk. If you plug a drive into a computer that does not understand its file system, the computer reports the drive as unreadable or offers to format it. Formatting creates a new empty file system, so choose carefully when the drive contains data.

Inodes and blocks

How does a file system actually store a file? The disk space is divided into fixed-size blocks (often 4 KiB). A file is stored in as many blocks as it needs, not necessarily next to each other. Where do the details live? In Unix-style file systems, in a small record called an inode that holds the file's metadata and the list of blocks it occupies. The file's name is not in the inode: it lives in the directory, which maps names to inode numbers. That is why a file can have several names (hard links) and why renaming within one disk is instant: only a directory entry changes, no data moves.

Two other structures complete the picture: a free-space map (a bitmap saying which blocks are free) and the directory entries. NTFS uses a similar idea with a master file table; FAT uses a table that chains blocks together. The details differ, but every design answers the same questions: where are the bytes, what are they called, and what is free?

Because files use whole blocks, a 1-byte file still takes one block. The unused tail is slack space. Many tiny files waste more space than their sizes suggest, which is why "size" and "size on disk" differ in file properties.

Deleting versus erasing

Here is the practical surprise. When you delete a file, the file system does not wipe its blocks. That would take as long as writing the file. It just removes the directory entry and marks the blocks as free in the free-space map. The old bytes stay on disk until something else is written over them.

That is why recovery tools can often bring back a deleted file, and why an immediately restored file works if you act quickly and stop using the drive. It is also why simply deleting a file is not a safe way to get rid of private data. Secure erasure means overwriting the blocks (on a hard disk), or using the drive's secure-erase or encryption features (on SSDs, which move data around internally to wear evenly, so overwriting a file does not reliably reach the old copy). Full-disk encryption makes the question easier: destroy the key and the leftover bytes become meaningless.

The second stand-in models a tiny disk of eight-byte blocks with a free map so you can watch a file being stored, deleted and partially overwritten.

JS Blocks, a free map, deleting and overwriting

Press Run (or Ctrl + Enter) to see the console output here.

No output.

After the delete, the "free" blocks still show the old bytes. The new file reused blocks 0 and 1 and wiped them, so only the final ! of the secret survives. That is how recovered files end up partly damaged. In the model, the inode list (inodes) is what ties a name to its blocks; once it is removed, the blocks are just anonymous free space.

What is real and what is a stand-in? Directories, paths, permissions, inodes, blocks, journals and the delete-then-reuse behaviour are real. The code keeps everything in JavaScript objects and a toy eight-block "disk"; real file systems use disk caches, journals, directory indexes (often B-trees) and block sizes of kilobytes, and they are written in kernel code.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between an absolute and a relative path?

    An absolute path names a file from the root of the tree. A relative path depends on where you currently are.
  2. 2. In a Unix-style file system, where is a file's name stored?

    Directories map names to inode numbers; the inode holds metadata and block locations but not the name.
  3. 3. What does deleting a file normally do?

    Quick deletion changes only metadata, so the old data may be recoverable until it is overwritten.
  4. 4. What is the purpose of a journal in a file system such as NTFS or ext4?

    Logging changes first lets the file system complete or roll back an interrupted operation.
  5. 5. A 1-byte file shows a size on disk of 4 KiB. Why?

    A file occupies at least one whole block, so tiny files waste the rest of that block.

Summary

  • A file system turns raw disk blocks into files and directories, tracks free space and stores metadata.
  • Directories form a tree from a root; paths can be absolute or relative, with . and .. as shortcuts.
  • Metadata includes size, times, owner and permissions, which the system checks on each access.
  • FAT32, exFAT, NTFS, ext4 and APFS differ in limits, features and the platforms they suit; journals protect against crashes.
  • Inodes hold metadata and block lists, directories hold names, and files use whole blocks, leaving slack space.
  • Deleting only frees blocks; secure disposal needs overwriting, secure erase or encryption.

Now move from one computer to many: Networking Basics.