File System in OS

The file system is part of the OS. It maintains the files on a hard disk. A file is a collection of sequence records stored on a Hard disk. The sequence of records means the sequence of bits, bytes, functions, and lines or records. When OS creates different files, it generates the code to understand these file structures.

File Structure and its Types

A File Structure must be according to the format that the OS can understand. Different files and their structures are

i. Text file: It is a sequence of characters that are organized into lines.

ii. Source file: It is a sequence of procedures and functions.

iii. Object file: It is a sequence of bytes organized into blocks that the machine can understand.

Some operating systems like UNIX and MS-DOS support fewer file structures.

File Types in MS-DOS and UNIX

MS-DOS and UNIX OS have the following types of files.

i. Ordinary files: These files contain the user information, which is text, databases, or executable programs. Users can apply the following operations on such files.

  • add
  • modify
  • delete
  • Remove the entire file.

ii. Directory files: Directory files contain a list of file names and other information related to these files, which are stored in the directory.

iii. Special files: Special files contain pieces of information about physical devices like disks, printers, networks, terminals, etc.

Special files are further divided into two types.

         i. Character special files: The data in character special files is handled character by character, as is the case with printers or terminals.

         ii. Block special files: The data in block special files is handled in blocks as in a Hard disk case.

Attributes of the File

i. Name: Each file has a specific name to recognize this file in the file system. Two or more files with the same name cannot be present in one directory.

ii. Identifier: Along with the name, Every File has its own extension, which indicates the file type. Suppose that in the filename “xyz.txt,” the xyz is the filename, and .txt is the file extension. Other file extensions may be like that

File Type Extension
MS Word .doc
audio .mp3
video .mp4
compressed file .zip
Database file .db
MS DOC .com
Image .jpg, .png, .jpeg
Plane Text .txt
Windows system .sys



iii. Type: Files are categorized into different types such as text, audio, video, database, system, executable, etc. files.

iv. Location: As we know, secondary storage is divided into many sectors. In the same way, the file is also divided into several blocks. The file block size is equal to the sector size of secondary memory so that the file can easily accommodate secondary storage. Each sector on secondary memory is considered as a location to store the blocks of the files.

v. Size: The file’s size is the file’s capacity required to accommodate secondary storage.

vi. Protection: A protection on file is a permission that is applied by the admin for different groups of Users to access the system.

vii. Time and Date: Every file contains the time and the date on which the file was created and last modified.

Operations on the File

The file system in OS performs several operations on the file. All everyday operations on file are.

i. Create: In a file system, creating a file is the primary task on the file. Different types of files are created for other purposes, such as,

  • Word processors are used to create a word file
  • Image editors are used to create the image files.
  • Text editors are used to create a text file.

ii. Write: The OS uses the Pointers to find the required position in the file from where the data needs to be written.

iii. Read: Files can be opened in three ways

  • Read
  • Write
  • append

A Read pointer is used to read the required file.

 iv. Reposition: According to the user’s requirement, moving pointers forward or backward in the file system is called repositioning. As in repositioning, OS is trying to find the actual address of the file, so It is also known as seeking.

 v. Delete: Deleting the file means deleting the file along with all its attributes. Allocated space to the file has also become available, and now it can be allocated to any other file.

vi. Truncate: Truncating the file means deleting it without deleting its attributes. After Transforming the file, its attributes remain in the storage location.

File Access Methods

The way in which the records of a file are accessed is called the file access mechanism. There are several ways to access files

  • Sequential access
  • Direct/Random access
  • Indexed sequential access

i. Sequential access

sequential access in file system

Most files, such as text files, audio files, video files, etc, need to be accessed sequentially because in the sequential access method, the operating system reads the file word by word. If there are 100 number instructions and OS wants to access the last block, then OS has to access the first 1 to 99 blocks to get the last block (100) in the file system.

In files like text, audio, and video, OS requires the very next word after accessing the previous. So, the sequential access method is used.

Compilers also use this access method. A tape drive is a common example of sequential access.

ii. Direct/Random access

Random access in file system

Random access is a method in which the records are accessed directly. In the random access method, there is no need to access the data sequentially. It is mostly used where filtered information is required, i.e., in a database. As in the database, most filtered records are accessed; soential access will be a slow procedure in such cases.

If there are 100 numbers of blocks and the OS wants to access the last one, then the OS can directly access it without accessing the first 1 to 99 blocks in the file system.

iii. Indexed sequential access

This method is built on the base of sequential access. In index sequential access, a file can be sorted on any sector/block in secondary memory. Then, an index block is created with all pointers (gives the actual address) to files. Each file has its own index block, which is also stored in a sector of secondary memory to access any particular block from the file.

If the file size is too large, then the number of indexes for each file block is also increased. Due to this, indexes of the whole file cannot be stored in the single sector of secondary storage. In such cases, multilevel indexing comes to occur.

In indexed accessing, searching in an extensive database is very fast and easy to implement. However, storing the index value requires some extra space in the memory.

Note: Some modern systems use the method of direct access and indexed access, but sequential access is also used in some cases where the need is to access the data sequentially, i.e., compilers.

What is a directory?

The directory contains the related files list and exists in secondary storage (HD). Some of the file’s attributes are also stored in the directory.

A hard disk can be divided into several partitions of different sizes. The partitions are also known as volumes, like Volume C, D, etc.

Each Volume or Directory must have at least one directory to list the number of files in it.

The directory contains several entries. Each entry represents a file with its attributes, as shown in the following diagram.Data-inside-directory-in-hard-disk

Data inside the directory is also known as metadata of the directory.

Operations on Directory

  1. File Creation
  2. Search for the file
  3. File deletion
  4. Renaming the file
  5. Traversing Files
  6. Listing of files