# File systems, inodes and directory structure

> How does a Unix file system store files? What is an inode, how do direct and indirect pointers work, and what is the difference between hard and soft...

Canonical: https://www.internhack.in/learn/interview/operating-systems-interview/os-file-systems-and-inodes
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

How does a Unix file system store files? What is an inode, how do direct and indirect pointers work, and what is the difference between hard and soft links?

## Answer

A file system is the part of the OS that turns a raw array of disk blocks into named, organized, persistent files. The user wants names, folders and permissions; the disk offers only numbered blocks. Think of a library. The books are the data blocks, the catalogue card for each book records where it lives and who can borrow it (the inode), and the index of titles that points to the cards is the directory.

In Unix-style file systems (ext4, ext2, UFS), every file is described by an inode (index node), a fixed-size on-disk structure. It stores the file's metadata: type (regular, directory, symlink, device), owner and group IDs, permission bits, size, timestamps (access, modify, change), link count, and pointers to the data blocks. What it does not store is the file name. That surprises many people. Names live in directories. A directory is itself a special file whose contents are a list of (name, inode number) pairs. To resolve /home/alice/notes.txt, the kernel starts at the root inode, reads the root directory's data, finds the entry for home, gets its inode number, reads that directory, and so on down the path.

How does the inode locate the data? The classic design has 12 direct pointers straight to data blocks, one single indirect pointer to a block full of pointers, one double indirect pointer (a block of pointers to blocks of pointers) and one triple indirect pointer. This is efficient because small files, which are the majority, need no extra reads, while the structure scales to huge files. Take a 4 KB block and 4-byte pointers, so a block holds 1,024 pointers. Direct: 12 x 4 KB = 48 KB. Single indirect: 1,024 x 4 KB = 4 MB. Double indirect: 1,024^2 x 4 KB = 4 GB. Triple indirect: 1,024^3 x 4 KB = 4 TB. The maximum file size is roughly 4 TB plus small terms. Modern ext4 replaces block lists with extents (a start block and a length), which are more compact for large contiguous files.

The on-disk layout has a superblock (file system parameters), an inode bitmap and data block bitmap tracking free space, the inode table, and the data blocks. The number of inodes is fixed when the file system is created, so you can run out of inodes ("No space left on device") even with free blocks if you create millions of tiny files.

Links are a favourite topic. A hard link is another directory entry pointing to the same inode; the inode's link count increases. The file's data is deleted only when the link count reaches zero (and no process has it open). Hard links cannot cross file systems, since inode numbers are only unique within one, and normally cannot link directories. A soft (symbolic) link is a separate small file whose content is a path name; it can cross file systems and point to directories, but it dangles if the target is removed.

Also cover allocation methods briefly: contiguous (fast, but external fragmentation), linked (FAT uses a table of links, no random access in the pure form), and indexed (inode approach, supports random access). Journaling file systems such as ext4 and NTFS log metadata changes before applying them, so a crash can be recovered quickly by replaying the journal instead of scanning the whole disk with fsck.

## Code examples

### Hard link versus symbolic link

```
$ echo "hello" > a.txt
$ ln a.txt hard.txt          # hard link: same inode
$ ln -s a.txt soft.txt       # symbolic link: new inode holding a path
$ ls -li a.txt hard.txt soft.txt
131074 -rw-r--r-- 2 user user 6 Sep 29 10:00 a.txt
131074 -rw-r--r-- 2 user user 6 Sep 29 10:00 hard.txt
131075 lrwxrwxrwx 1 user user 5 Sep 29 10:00 soft.txt -> a.txt
$ rm a.txt
$ cat hard.txt
hello
$ cat soft.txt
cat: soft.txt: No such file or directory
```

a.txt and hard.txt share inode 131074 with link count 2, so the data survives deleting a.txt. soft.txt has its own inode and dangles.

## Key points

- An inode holds metadata and block pointers but not the file name.
- Directories map names to inode numbers.
- 12 direct plus single, double, triple indirect pointers allow a very large maximum file size.
- Hard links share an inode and cannot cross file systems; symlinks store a path.
- Journaling speeds crash recovery by replaying logged metadata changes.

## Common follow-up questions

- Can you run out of inodes with free disk space?
- What happens when you delete a file that a process still has open?
- What is the difference between FAT and inode-based allocation?
- What does journaling protect against?

## Interview tips

- Say early that the file name is not in the inode; it shows real understanding.
- Compute the maximum file size for a given block size.
- Distinguish hard and soft links using the link count.

## Concepts covered

inode, file system, block allocation, hard link, symbolic link

Previous: [Thrashing and Belady's anomaly](https://www.internhack.in/learn/interview/operating-systems-interview/os-thrashing-and-beladys-anomaly) · Next: [Disk scheduling: FCFS, SSTF, SCAN and C-SCAN](https://www.internhack.in/learn/interview/operating-systems-interview/os-disk-scheduling-algorithms) · [All of Operating Systems](https://www.internhack.in/learn/interview/operating-systems-interview)

## More Operating Systems questions

- [What is an Operating System and what are its main functions?](https://www.internhack.in/learn/interview/operating-systems-interview/os-what-is-an-operating-system)
- [Explain kernel mode vs user mode and how system calls work](https://www.internhack.in/learn/interview/operating-systems-interview/os-kernel-mode-user-mode-system-calls)
- [Monolithic kernel vs microkernel: differences and trade-offs](https://www.internhack.in/learn/interview/operating-systems-interview/os-monolithic-vs-microkernel)
- [Program vs process: what is the difference?](https://www.internhack.in/learn/interview/operating-systems-interview/os-process-vs-program)
- [Process states, state transitions and the Process Control Block](https://www.internhack.in/learn/interview/operating-systems-interview/os-process-states-and-pcb)
- [Process vs thread: key differences](https://www.internhack.in/learn/interview/operating-systems-interview/os-process-vs-thread)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Operating Systems](https://www.internhack.in/learn/interview/operating-systems-interview)
