EthicsComputerCourses
Join training +91 90532 10052

Files, Directories & File Systems

Operating Systems · Lesson 24 of 32 Advanced 8 min read Updated

By the end of this lesson you will be able to describe what the OS stores about a file, compare directory structures, resolve absolute and relative paths, explain hard and soft links, and name the file systems used by Linux, Windows and phones.

RAM forgets everything when the power goes off, so anything worth keeping lives in files on storage. A file system is the part of the OS that turns raw storage blocks into named files and folders you can find, protect and share.

What is a file?

To the OS, a file is a named collection of related data stored on secondary storage. The OS does not care whether it is a song, a spreadsheet or a Python program; it sees a sequence of bytes plus some information about those bytes.

That information is the file's attributes (also called metadata):

AttributeExample
Namemarksheet.pdf
TypeOften hinted by the extension, such as .pdf
Size248,513 bytes
LocationWhich storage blocks hold the data
Owner and permissionsOwner meera; she can read and write, others can only read
TimestampsCreated, last modified, last accessed

The OS also provides file operations through system calls: create, open, read, write, seek (move the read/write position), close, delete and rename. When a process opens a file, the kernel keeps an open-file table entry with the current position, which is what the file descriptor you met in System Calls points to.

Access methods

  • Sequential access: read the bytes in order, from start to end, like playing a cassette tape. Most text processing and log reading works this way.
  • Direct (random) access: jump to any position and read there, like choosing any track on a CD. Databases need this to fetch record number 7,000 without reading the first 6,999.
  • Indexed access: keep an index (like a book's index) that maps keys to positions, then jump directly. Database indexes build on this idea.

Directory structures

A directory (folder) is a special file that lists other files and directories. Over time, operating systems used several designs.

StructureIdeaProblem it solves or has
Single-levelOne directory for everyoneName clashes: two users cannot both have notes.txt
Two-levelOne directory per userNo subfolders, hard to share between users
TreeDirectories inside directories, one rootClean and flexible; used everywhere today
Acyclic graphA tree plus links, so one file can appear in two placesSharing works; deleting needs care
General graphLinks can form cyclesSearching can loop for ever; usually avoided

A tree of directories is like the classic Indian postal address: State, District, City, Locality, House. Each level narrows the search, and two people can have the same house number in different cities.

Paths: absolute and relative

A path names a file by the directories you walk through to reach it.

  • An absolute path starts from the root: /home/meera/notes.txt on Linux, C:\Users\Meera\notes.txt on Windows.
  • A relative path starts from the current working directory: from /home/meera, the path notes.txt means the same file.
  • . means "this directory" and .. means "the parent directory".

Step 1: run this small path resolver. It walks the tree exactly as the kernel does: one name at a time, from the root or from the current directory.

PythonRun it yourself
tree = {"home": {"meera": {"notes.txt": "file", "projects": {"os.py": "file"}},
                 "rohan": {"todo.txt": "file"}},
        "etc": {"hosts": "file"}}

def resolve(path, cwd):
    absolute = path.startswith("/")
    stack = [] if absolute else cwd.strip("/").split("/")
    for part in path.split("/"):
        if part in ("", "."):
            continue
        if part == "..":
            if stack: stack.pop()
        else:
            stack.append(part)
    node = tree
    for name in stack:                       # walk down the tree one directory at a time
        if not isinstance(node, dict) or name not in node:
            return f"{path!r}: No such file or directory"
        node = node[name]
    kind = "directory" if isinstance(node, dict) else "file"
    return f"{path!r} -> /{'/'.join(stack)} ({kind})"

cwd = "/home/meera"
for p in ["notes.txt", "projects/os.py", "../rohan/todo.txt", "/etc/hosts", "./../../etc", "../priya"]:
    print(resolve(p, cwd))
Output
'notes.txt' -> /home/meera/notes.txt (file)
'projects/os.py' -> /home/meera/projects/os.py (file)
'../rohan/todo.txt' -> /home/rohan/todo.txt (file)
'/etc/hosts' -> /etc/hosts (file)
'./../../etc' -> /etc (directory)
'../priya': No such file or directory

Step 2: trace ./../../etc by hand from /home/meera. . stays put, the first .. goes to /home, the second goes to /, then etc gives /etc.

Working with real files from Python

Python's pathlib module asks the OS to create directories and files, and to list them. The browser editor gives you a temporary in-memory file system, so this really runs.

PythonRun it yourself
from pathlib import Path

root = Path("college")
(root / "sem3" / "os").mkdir(parents=True, exist_ok=True)
(root / "sem3" / "dbms").mkdir(parents=True, exist_ok=True)
(root / "sem3" / "os" / "notes.txt").write_text("Paging: pages map to frames\n", encoding="utf-8")
(root / "sem3" / "dbms" / "er.txt").write_text("Entities and relationships\n", encoding="utf-8")

for path in sorted(root.rglob("*")):
    if path.is_dir():
        print("dir ", path.as_posix())
    else:
        text = path.read_text(encoding="utf-8")
        print("file", path.as_posix(), f"({len(text)} characters)")
Output
dir  college/sem3
dir  college/sem3/dbms
file college/sem3/dbms/er.txt (27 characters)
dir  college/sem3/os
file college/sem3/os/notes.txt (28 characters)

Each mkdir, write_text and directory listing turns into system calls such as mkdir, openat, write and getdents64 on Linux.

Links let one file appear under two names, which makes the directory structure an acyclic graph.

  • A hard link is a second directory entry pointing to the same file data (the same inode, explained in the next lesson). The data is deleted only when the last hard link is removed.
  • A soft link (symbolic link, or symlink) is a tiny file that stores a path to another file. If the target is deleted, the symlink is left "dangling" and broken.

On Linux you can see this with ln and ls -li (the first column is the inode number, the third is the link count):

Terminal
ln marks.txt marks-hard.txt
ln -s marks.txt marks-soft.txt
ls -li marks*
Output
524301 -rw-r--r-- 2 meera meera  8 Nov  2 10:15 marks-hard.txt
524318 lrwxrwxrwx 1 meera meera  9 Nov  2 10:15 marks-soft.txt -> marks.txt
524301 -rw-r--r-- 2 meera meera  8 Nov  2 10:15 marks.txt

marks.txt and marks-hard.txt share inode 524301 and have a link count of 2. The symlink has its own inode. Windows supports both ideas too: NTFS hard links and symbolic links (mklink), plus the older desktop shortcut (.lnk) file, which is just a normal file the shell understands.

Mounting and common file systems

Storage devices are attached to the directory tree by mounting. On Linux, a USB drive might be mounted at /media/meera/PENDRIVE, so it appears as a folder. Windows gives each volume a drive letter such as D:, though it can also mount volumes into folders.

File systemUsed byNotes
ext4Default on many Linux distributionsJournaling, extents, mature and reliable
XFS, BtrfsLinux servers and some desktopsXFS for large files; Btrfs adds snapshots and checksums
NTFSWindows system drivesJournaling, permissions (ACLs), compression
ReFSWindows Server storageBuilt for integrity and large volumes
FAT32, exFATPen drives, SD cards, camerasSimple and widely supported; FAT32 limits files to 4 GB
APFSmacOS, iOSSnapshots, copy-on-write, encryption
F2FS, ext4Many Android phonesF2FS is designed for flash storage

Linux supports many file systems at once through the Virtual File System (VFS) layer. Programs call the same open, read and write, and the VFS passes each call to the right file-system driver underneath.

Common mistakes

  • Treating the extension as the file type. Renaming photo.jpg to photo.pdf changes nothing inside the file; the extension is only a hint.
  • Mixing up hard and soft links. Deleting the original breaks a soft link but not a hard link.
  • Using \ in Linux paths or forgetting it on Windows. Linux uses /; Windows accepts both in most APIs but shows \.

Key points

  • A file is a named sequence of bytes plus attributes such as size, owner, permissions and timestamps.
  • Access can be sequential, direct or indexed.
  • Modern systems use tree-structured directories with links, forming an acyclic graph.
  • Absolute paths start at the root; relative paths start at the current directory; .. is the parent.
  • Hard links share the same file data; soft links store a path and can dangle.
  • Linux mounts devices into one tree and uses the VFS layer to support ext4, XFS, Btrfs, NTFS and more.

Exercise

Using the path resolver, set cwd = "/home/rohan" and resolve these paths: todo.txt, ../meera/projects/../notes.txt, /home/meera/projects/os.py/.. and ../../home/./rohan. Predict each answer before you run the code.

Show solution
Python
cwd = "/home/rohan"
for p in ["todo.txt", "../meera/projects/../notes.txt", "/home/meera/projects/os.py/..", "../../home/./rohan"]:
    print(resolve(p, cwd))
Output
'todo.txt' -> /home/rohan/todo.txt (file)
'../meera/projects/../notes.txt' -> /home/meera/notes.txt (file)
'/home/meera/projects/os.py/..' -> /home/meera/projects (directory)
'../../home/./rohan' -> /home/rohan (directory)

Run it after the resolver example. The third path is a trap: this simple resolver removes os.py when it meets ... A real Linux kernel would reply "Not a directory", because you cannot step into a file. Real path lookup checks each part as it goes.

Quick Quiz

1. Which path is absolute on Linux?

2. A file has two hard links. One is deleted. What happens to the data?

3. Which access method lets a database jump straight to record 7,000?

4. What does the Linux VFS layer do?

Learn Operating Systems with a mentor

Free lessons get you started. Our Computer Science Fundamentals training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call