File System in Operating System
Subject: Computer Science Fundamentals · Language: Operating System · Level: intermediate · 10 min read
Learn files, directories, file attributes, allocation methods, access methods, permissions, and how operating systems organize storage.
A file system is the part of the operating system that manages how data is stored, named, organized, accessed, and protected on storage devices. Without a file system, data on a disk would be only a large collection of raw blocks with no clear names or structure. A beginner can think of a file system like a library arrangement. Books need names, shelves, categories, indexes, and rules for borrowing. In the same way, files need names, folders, locations, metadata, and permissions. The file system provides this organization.
The file system hides the low-level details of storage hardware. A user simply opens a file by name, but internally the OS must locate the file's blocks on disk, check permissions, read the data, and return it to the program.
What Is a File?
A file is a named collection of related data stored on a secondary storage device such as a hard disk, SSD, pen drive, or memory card. Examples of files include text documents, images, videos, programs, spreadsheets, PDFs, source code files, and database files. From the user's point of view, a file has a name and content. From the operating system's point of view, a file also has metadata such as size, location, owner, permissions, and timestamps.
Why Files Are Needed
RAM loses data when the computer is switched off, but secondary storage can keep data permanently. Files allow users and programs to store data for future use. Files also make data easier to manage. Instead of remembering physical disk block numbers, users can work with meaningful names such as `notes.txt`, `photo.jpg`, or `report.pdf`. The file system provides a bridge between human-friendly names and hardware-level storage blocks.
File Attributes
File attributes are details stored about a file. These details help the operating system manage and protect the file. Common attributes include file name, file type, size, location, owner, permissions, creation time, modification time, and last access time. For example, when you check a file's properties in Windows or Linux, you are viewing some of these attributes. The OS stores and updates this information automatically.
File Operations
The operating system provides basic operations to work with files. These operations are used by users directly and by programs through system calls. Common file operations include create, open, read, write, close, delete, rename, copy, move, and change permissions. Opening a file prepares it for use. Reading gets data from the file. Writing stores new data into the file. Closing releases the file after the program finishes using it.
File Types
Different files store different kinds of data. A text file stores readable characters, while an image file stores pixel information in a format such as PNG or JPEG. Executable files contain instructions that the operating system can load and run. Directory files store information about folders and file names. Many operating systems use file extensions to help identify file type. For example, `.txt` usually represents a text file, `.jpg` an image, `.mp4` a video, and `.exe` an executable file on Windows.
Directory
A directory, also called a folder, is used to organize files. It can contain files and other directories. Directories make storage easier to navigate. For example, a student may keep notes in one folder, assignments in another folder, and projects in a separate folder. Internally, a directory stores mappings between file names and file information. When a user opens a file by name, the OS searches the directory structure to find it.
Directory Structure
A file system usually organizes directories in a tree structure. The top-level directory is called the root directory. Inside the root directory, there can be many subdirectories. Each subdirectory can contain more files and folders. This creates a hierarchy. For example, a path like `/home/student/notes/os.txt` shows that `os.txt` is inside `notes`, which is inside `student`, which is inside `home`.
Absolute and Relative Path
An absolute path gives the complete location of a file from the root directory. It does not depend on the current working directory. A relative path gives the location of a file from the current directory. It is shorter, but its meaning depends on where the user or program is currently located. For beginners, this difference is important when using command-line interfaces. A file may exist, but the command may fail if the path is written incorrectly.
File Control Block
A file control block is a data structure used by the operating system to store important information about a file. It contains metadata needed to manage the file. This information may include file size, location of data blocks, permissions, owner details, timestamps, and other control data. The exact name and structure may differ between operating systems, but the purpose is the same: the OS needs a reliable record for each file.
File Allocation
File allocation means deciding how the blocks of a file are stored on disk. Since a file may be larger than one disk block, the OS must track all blocks belonging to that file. Good allocation should use disk space efficiently and allow files to be read and written quickly. There are three common allocation methods taught in operating system basics: contiguous allocation, linked allocation, and indexed allocation.
Contiguous Allocation
In contiguous allocation, a file is stored in consecutive disk blocks. If a file needs five blocks, the OS stores those five blocks side by side. This method is simple and fast for sequential reading because the next block is located immediately after the current block. The disadvantage is external fragmentation. Over time, free space may become scattered, and it may be hard to find a large continuous area for a new file.
Linked Allocation
In linked allocation, each file is stored as a linked list of disk blocks. Each block points to the next block of the same file. This avoids the need for contiguous free space. A file can grow by adding blocks from anywhere on the disk. The disadvantage is slower direct access. To reach a particular block in the middle of a file, the OS may need to follow pointers from the beginning.
Indexed Allocation
In indexed allocation, each file has an index block that stores the addresses of all blocks belonging to the file. This makes direct access easier because the OS can use the index block to find any part of the file. It is more flexible than contiguous allocation and more direct than linked allocation. The disadvantage is that extra space is needed for index blocks. For very small files, this overhead may feel large compared to the actual data.
File Access Methods
A file access method defines how data inside a file is read or written. The two common access methods are sequential access and direct access. In sequential access, data is processed in order from beginning to end. This is useful for logs, text reading, and media streaming. In direct access, the program can jump to a specific part of the file. This is useful for databases and large files where a program may need one record without reading everything before it.
File Permissions
File permissions control who can read, write, or execute a file. Permissions are important for security and data protection. For example, a user may be allowed to read a file but not modify it. A program file may need execute permission before it can run. In multi-user systems, permissions prevent one user from accidentally or intentionally changing another user's files.
Mounting
Mounting is the process of making a file system available to the operating system at a particular location in the directory tree. For example, when a pen drive is connected, the OS detects its file system and makes it accessible through a drive letter or mount point. After mounting, users can browse files normally. Before removal, the device should be safely unmounted or ejected so pending writes can finish properly.
Free Space Management
The file system must track which disk blocks are free and which blocks are already used. This is called free space management. Common methods include bitmaps and free lists. A bitmap uses bits to mark blocks as free or used. A free list stores links to available blocks. Without free space management, the OS would not know where to place new files or expanded file data.
Journaling File System
A journaling file system keeps a record of changes before applying them fully. This helps protect the file system from corruption if the computer shuts down suddenly during a write operation. For example, if power fails while a file is being updated, the journal can help the OS recover the file system to a consistent state. Journaling is used in many modern file systems because reliability is important for real computers and servers.
Common File Systems
Different operating systems support different file systems. Windows commonly uses NTFS. Linux commonly uses file systems such as ext4. Removable drives often use FAT32 or exFAT for compatibility. Each file system has its own design, features, limits, and performance behavior. Some focus on compatibility, some focus on reliability, and some focus on performance.
File System and Performance
File system performance depends on allocation method, caching, disk speed, fragmentation, file size, and access pattern. Reading a file sequentially is usually faster than jumping randomly across many disk locations. SSDs reduce some seek-time problems, but organization still matters. Operating systems also use caching to keep frequently used file data in memory. This reduces repeated disk access and improves speed.
Summary
A file system organizes data on storage devices using files, directories, paths, metadata, permissions, and allocation methods. It makes storage understandable and usable for both users and programs. For beginners, the most important idea is that a file name is only the visible part. Behind that name, the OS manages blocks, metadata, permissions, free space, and reliability so that data can be stored and retrieved correctly.
More Operating System Lessons
- Introduction to Operating System
- Types of Operating System
- Operating System Functions and Services
- System Calls in Operating System
- Process Management in Operating System
- Process Scheduling in Operating System
- CPU Scheduling Algorithms
- Threads and Multithreading in Operating System
- Process Synchronization in Operating System
- Deadlock in Operating System
- Memory Management in Operating System
- Virtual Memory in Operating System