File System β€” Easy Exam Guide Unit u03 (Files + Directories). Every idea is explained in simple words and linked to how the exam asks it. πŸ”΄ = comes a lot Β· 🟑 = comes sometimes Β· βšͺ = rare.

Written in simple (B1) English. Read it from top to bottom before the exam.

0How "files" come in the exam

The file system is one of the easiest topics to get points. It is usually 1 or 2 multiple-choice questions, about 2–2.5 points each. You only need to remember facts and do a little simple math. There are just six kinds of questions. This guide makes each one easy.

The question you will seeWhere it comes from in u03Section
πŸ”΄ A row of 0/1 blocks β†’ "which sentences are true?" (allocation, fit, fragmentation)Directories slides β€” allocationΒ§5
πŸ”΄ ASCII / Unicode / UTF-8 / binary true-falseFiles slides β€” encodingΒ§1
πŸ”΄ A list of ln/rm/echo/cat commands β†’ what is the content, size, link count?Directories slides β€” linksΒ§4
πŸ”΄ open/read/write/close true-falseFiles slides β€” POSIX I/OΒ§2
🟑 opendir/readdir and stat/lstat/fstat behaviourDirectories slides β€” callsΒ§7
🟑 Read an ls -l line (permissions, size, link count)Directories slides β€” UNIX FSΒ§8
⚠️ Always remember
Every file question is multiple-choice, and a wrong answer takes points away. The questions put false "trap" sentences next to true ones. Only tick a box when you are sure.

1Files and character encoding πŸ”΄ comes a lot

What a file really is

A file is just a line of bytes. The Linux kernel does not know if a file is "text" or "binary". This difference is only in how you read the bytes, not in the file itself.

Text (ASCII) files

Unicode and UTF-8

Binary files

🎯 In the exam β€” encoding true/false (2025-02-17 Ex1, 2024-09-18 Ex1, 2024-02-27 Ex8)
These five sentences are TRUE almost every time:
  • UTF-8 has variable length and works with ASCII.
  • Unicode contains ASCII (it is a superset).
  • Binary files are not easy to read and can change on a different computer.
  • A Unicode text file can be bigger than the same text in ASCII.
  • If you read a UTF-8 file as ASCII, you see strange characters.
βœ— Trap sentences (these are FALSE)
  • "ASCII has all modern languages." No β€” only ~128 characters.
  • "Unicode is always 32 bits" / "all Unicode fits in 2 bytes." No β€” the length changes.
  • "ASCII is variable length." No β€” it is fixed length.
  • "Unicode and UTF-8 are the same." No β€” set vs encoding.
  • "A number in ASCII and binary has the same bytes." No.

2Reading and writing files β€” two libraries πŸ”΄ comes a lot

In C there are two ways to read and write files. The exam tests the difference between them.

ISO C library (with buffer)POSIX system calls (no buffer)
HandleFILE * (a stream)an int file descriptor (fd)
BufferYes (but stderr has no buffer; use fflush)No β€” every call goes to the system
Open/closefopen, fcloseopen, close
Read/writefgetc/fputc, fgets/fputs, fscanf/fprintf, fread/fwriteread, write, lseek
Standard filesstdin/stdout/stderr0=stdin, 1=stdout, 2=stderr

The five system calls β€” this is what the exam asks

All UNIX file work uses five calls: open, read, write, lseek, close.

open() β€” the "how many parameters" trap

int open(const char *path, int flags);              // 2 parameters
int open(const char *path, int flags, mode_t mode); // 3 parameters, when you create a file

read() and write() β€” the "what it returns" trap

int read (int fd, void *buf, size_t nbytes);
int write(int fd, void *buf, size_t nbytes);

lseek() and close()

off_t lseek(int fd, off_t offset, int whence);  // whence: SEEK_SET / SEEK_CUR / SEEK_END
int   close(int fd);
🎯 In the exam β€” open/read/write/close (2025-09-17 Ex1, 2023-02-03 Ex1, 2025-06-12 Ex5, 2022-09-06 Ex1)
TRUE sentences: read/write work on any bytes and are the same for file/screen/pipe; open() can set permissions when it creates a file; printf uses write. Note: 2025-09-17 Ex1 and 2023-02-03 Ex1 are exactly the same question β€” learn it one time.
βœ— Trap sentences (FALSE)
  • "open() takes exactly 2 / exactly 3 parameters." It can take 2 or 3 (both phrasings are false).
  • "read() returns the number of objects / bits." It returns bytes.
  • "A cyclic-graph file system does not allow sharing." Graphs are made to allow sharing.

3Types of directories & the idea of links 🟑 helps you understand §4

A directory is a node in a tree that keeps information about its files. We use directories for three reasons: speed, names, and grouping. The slides show how directories grew over time. You mainly need the last two types:

TypeIdeaProblem
One levelall files in one directorynames must be different; no multi-user
Two levelsone directory for each useryou need a path; no grouping
Treedirectories inside directories (current dir, paths)cannot share one file under two names
Acyclic graphyou can share: one file has many names/paths, using linksyou must be careful when you delete β†’ soft vs hard links
Cyclic graphcycles are alloweddanger of infinite loops; usually you cannot link a directory

A link is a reference to a file that already exists. When you share a file, there are two ways to delete it β€” and these two ways are the two link types in Β§4:

4Hard links and soft links πŸ”΄ ~3/13 exams β€” always a command trace

This is the most "tricky" file question. You follow a list of ln/rm/echo/cat commands and you say what the content, size, and link count are. Learn the model below and it becomes easy points.

The simple model

In UNIX, a directory is a table: it maps a name β†’ an i-node number. The real data and information of the file live in the i-node. The i-node has a counter of names (link count).

Hard link ln f1 f2Soft / symbolic link ln -s f1 f2
What it isa second name for the same i-nodea small new file. Its data is the path of the target
Same content?Yes β€” same i-node, same data. Change one name β†’ both changeNo β€” it only points by the path name
Counter+1 for each hard link. Data is deleted only when the counter is 0does not change the target's counter
Sizesame as the file= length of the path text (e.g. "f1" β†’ 2 bytes)
If you delete the targetdata still lives, through the other hard linksthe link is now broken β†’ cat gives an error
Rulesyou cannot hard-link a directory, or link across file systemsyou can link a directory and cross file systems
β–Ά Example trace (like 2025-09-17 Ex5)
echo -n "hello_world" > f1   # f1: inode A, data "hello_world", count 1
ln f1 f2                     # f2 β†’ inode A, count 2
ln f1 f3                     # f3 β†’ inode A, count 3   ← (not 2! this is the trap)
ln -s f1 f4                  # f4: NEW inode, soft link, data = "f1" (size 2)
rm f1                        # delete the NAME f1; inode A count β†’ 2 (f2,f3 still here)
echo "hello" > f4            # f4 points to path "f1" β†’ it MAKES f1 again (new inode)
echo "_world" >> f2          # add to inode A β†’ now "hello_world_world"
Result: f2 and f3 have the same content "hello_world_world"; the size of f4 is 2 bytes; after the two ln commands the counter was 3; f1 exists again (made by the soft link); the hard-link count of f2 is 2.
🎯 In the exam β€” link traces (2022-02-10 Ex2 β†’ 2023-02-03 Ex5 β†’ 2025-09-17 Ex5)
These three are almost the same question; they only get longer over the years. The facts that always come back:
  • Hard links share one i-node and one content; if you add text with one name, all names change.
  • ln f1 f2; ln f1 f3 makes the counter 3 (the "= 2" option is a trap when a 3rd link exists).
  • A soft link's size = length of the path; it breaks when the target is deleted (the first cat gives an error), but writing through it can make the target again.
  • You cannot make a hard link to a directory.

5How files are placed on the disk (allocation) πŸ”΄ the most common FS question β€” ~5/13

You get the disk as a row of blocks (free = 0, full = 1). You must say which sentences are true. To do this, you think about three placement methods and three "fit" algorithms. Learn this part well and you own the topic.

The three placement methods

MethodHow it saves a fileDirectory keepsFragmentationAccess
Contiguousin blocks next to each otherstart block + lengthexternal (gaps) + internal (last block not full)line by line and direct
Linked (and FAT)blocks anywhere; each block points to the nextpointer to the first (and last) blockno external; but each block needs a pointeronly line by line is fast
Indexed (i-node)blocks anywhere; all pointers are in one index blockpointer to the index blockno external; but the index block costs spaceline by line and direct

The three "fit" algorithms β€” for contiguous placement

AlgorithmIt chooses…
First-fitthe first gap that is big enough (start from the left)
Best-fitthe smallest gap that still fits (less waste)
Worst-fitthe biggest gap (leaves a big free part)

When you place many files in order, these three can give different results (one works, another fails). That is the whole point of the question.

Fragmentation math

β–Ά How to do a bitmap question
  1. Write the index number over each bit. Find the runs of 0 (free) and their lengths.
  2. "Can a file of size S fit in contiguous?" β†’ you need one run β‰₯ round up (S / block size).
  3. "Can it fit in linked/indexed?" β†’ just compare round up (S / block size) with the total free blocks (indexed: minus 1 for the index).
  4. For a fit-algorithm list β†’ place each file in order, and remove the blocks you use.
  5. For internal fragmentation β†’ blocks Γ— block size βˆ’ file size.
Example (1 MB blocks): a 2.9 MB file needs round up (2.9) = 3 blocks. It fits only if there is a run of 3 free blocks. "Indexed, 17 free blocks" β†’ biggest file = 16 MB (1 block is the index).
🎯 In the exam β€” bitmap (2024-02-07 Ex1, 2024-06-20 Ex1, 2025-01-28 Ex7, 2023-02-17 Ex1, 2022-06-14 Ex3) + theory (2025-06-12 Ex2)
The facts that come back as TRUE: contiguous has BOTH internal and external fragmentation; linked has NO external fragmentation; contiguous biggest file = longest free run; indexed biggest file = total free βˆ’ 1 index block; internal fragmentation is less than one block.
βœ— Common traps
  • "Contiguous has internal but not external fragmentation." It has both.
  • "Indexed cannot save a file bigger than the longest run." Wrong β€” indexed uses blocks anywhere.
  • Mixing best-fit and first-fit on a list of files β€” do the steps, do not guess.
  • Forgetting the index block when you count indexed space.

6The UNIX i-node & free space 🟑 base for Β§4–§5

The i-node

UNIX/Linux uses indexed placement with a combined plan. Each file has an i-node. The i-node keeps information (mode, 2 owners, 3 times, size, block count) and 15 block pointers:

PointersTypeWhat it reaches
1–12directthe first 12 data blocks (small files)
13single indirecta block of pointers β†’ data
14double indirecta block of pointers β†’ blocks of pointers β†’ data
15triple indirectthree levels of pointers

So small files are fast (direct), but very big files are still possible (indirect). If one block holds k pointers (k = block size / pointer size), the biggest file β‰ˆ (12 + k + kΒ² + kΒ³) Γ— block size.

Why this matters for the exam
The theory question (2025-06-12 Ex2) asks exactly these facts: indexed placement has no external fragmentation, smaller blocks waste less, indirect blocks make the file bigger (they do not make access faster), and directories are files too β€” they also have an i-node (a trap says they do not).

Free-space management

The system remembers free blocks with a bitmap: one bit for each block (free or full). This bitmap is exactly the row of 0/1 that you see in the allocation questions.

The link count of a directory

A directory is also a file with hard links, so its link count follows a rule: an empty directory has count 2 (its name + its own . entry). It gets +1 for each sub-directory (each child has a .. that points back).

7Directory & information system calls 🟑 ~2/13 exams

File information: stat / lstat / fstat

int stat (const char *path, struct stat *sb);  // follows the soft link to the target
int lstat(const char *path, struct stat *sb);  // information about the LINK itself
int fstat(int fd,           struct stat *sb);  // by an open file descriptor

List a directory: opendir / readdir / closedir

DIR           *opendir (const char *path);  // returns a DIR* stream, or NULL
struct dirent *readdir (DIR *dp);           // ONE entry each call; NULL at the end or on error
int            closedir(DIR *dp);

Move and create

char *getcwd(char *buf, int size);   // the current directory
int   chdir (const char *path);
int   mkdir (const char *path, mode_t mode);
int   rmdir (const char *path);      // only if the directory is EMPTY
🎯 In the exam β€” call behaviour (2022-09-06 Ex2 opendir; 2022-06-14 Ex4 stat)
TRUE: opendir β†’ DIR*; readdir gives one struct dirent (with i-node) each call; stat/lstat/fstat have the same return value and show the i-node; stat = fstat for a normal file.
FALSE traps: "readdir returns all entries as a list", "readdir returns NULL only when empty", "opendir returns a struct dirent*", "lstat gives information about the target", "these calls can list a directory" (only opendir/readdir can).

8How to read an ls -l line 🟑 sometimes

Read the fields from left to right:

-rwxr-xr--   1   user3   user3   9876   Feb 8 12:36   script.sh
β”‚β””β”¬β”˜β””β”¬β”˜β””β”¬β”˜   β”‚     β”‚       β”‚       β”‚         β”‚            β”‚
β”‚ β”‚  β”‚  β”‚    β”‚   owner   group   size      time         name
β”‚ β”‚  β”‚  β”” others: r--    (the "1" is the link count β”€β”˜ above)
β”‚ β”‚  └─── group:   r-x
β”‚ └────── owner:   rwx
└──────── type:  - file, d directory, l soft link
🎯 In the exam β€” 2025-02-17 Ex4
Read each group carefully. One strange thing in the key: for drwxr-xr-x, the sentence "the directory is executable by all users" was marked false, even though others have x. Think of x on a directory as "can enter", and do not trust the key 100% here.

9Modern file systems βšͺ rare β€” just read fast

FSNotes
FAT (12/16/32), exFATthe linked/FAT family. FAT32 = 32-bit clusters; exFAT for USB/flash; VFAT adds long names. Simple, portable, no journaling.
NTFSthe Windows standard. Big sizes, journaling, encryption. Linux/macOS read it with drivers.
Ext / Ext2 / Ext3 / Ext4the Linux family. Ext2 = no journaling; Ext3 added journaling; Ext4 = bigger sizes + faster, still compatible.

Journaling (NTFS, Ext3/4) writes changes like small transactions, so the file system can repair itself after a crash. That is all you need here.

10One-page trap sheet

Encoding

  • Unicode contains ASCII Β· UTF-8 = 1–4 bytes & works with ASCII Β· UTF-16 does NOT work with ASCII.
  • "Unicode = UTF-8" βœ— Β· "ASCII variable length" βœ— Β· "Unicode always 32-bit / 2-byte" βœ—.
  • Binary: not easy to read, can change on another computer; a number in ASCII vs binary has different bytes and size.

System calls

  • open() takes 2 or 3 parameters β€” "exactly 2" βœ— and "exactly 3" βœ—.
  • read() returns BYTES (not objects, not bits). read/write are the same for file/screen/pipe. printf uses write.
  • lstat = the link itself; opendir β†’ DIR*; readdir β†’ one dirent each call, NULL = end OR error.

Links

  • Hard link = same i-node (same content, counter +1); soft link = a file with a path inside (size = path length, can break).
  • ln f1 f2; ln f1 f3 β†’ counter 3. You cannot hard-link a directory. Writing through a soft link can make the target again.

Allocation

  • Contiguous = both internal + external fragmentation; biggest file = longest free run; needs first/best/worst-fit.
  • Linked = no external fragmentation, pointer cost; FAT puts the pointers in one table.
  • Indexed = no external fragmentation; biggest file = total free βˆ’ 1 index block; UNIX i-node = 12 direct + single/double/triple indirect.
  • Internal fragmentation = round up(S/B)Β·B βˆ’ S, always less than one block. Directories have i-nodes too.