Understanding the Linux File System

Understanding the Linux File System

ยท

10 min read

Introduction

Welcome to our tech blog, where we dive deep into the intricacies of the Linux file system. Whether you're a beginner or an experienced user, understanding the Linux file system is essential for efficiently managing your files, directories, and storage resources.

In this comprehensive guide, we will explore the core concepts, structure, and features of the Linux file system.

Device

When you connect a device to the system, the system needs to communicate with the device to transfer data. For that, the system needs some device files, by using which system can communicate with the device. Here, device files come in. All device files are stored in the "/dev" directory.

Screenshot_2022-06-26_05_19_00.jpg

Let's understand some of the terminologies first

  • Permissions: Permissions indicate the type of access each set of users has to the file or directory.

  • Owner: The owner is the user who owns the file or directory and has certain privileges and permissions over it.

  • Group: The group refers to a collection of users who share common access permissions to the file or directory.

  • Major device number: The major device number identifies the driver or peripheral card associated with a device connected to the system.

  • Minor device number: The minor device number identifies a specific device within a driver or peripheral card.

  • Time: The time refers to when the file or directory was created or modified.

  • Device name: The device name indicates the specific device associated with the file.

  • Pseudo device: Pseudo devices are virtual devices that are not physically connected to the system, such as /dev/null and /dev/zero.

Permission bits

  • The different bits present in the permission indicate different things. If the first bit is "d," it signifies a directory. If it's a hyphen ("-"), it indicates a regular file. If it's "l," it represents a symbolic link. The meanings of "c," "b," "p," and "s" in the permission bits are:

  • c: Character device - This type of file is used for transferring data character by character.

  • b: Block device - Block devices transfer data in fixed-size blocks, such as hard disk drives (HDDs) and solid-state drives (SSDs).

  • p: Pipe - This file helps feed the output of one file into the input of another file using pipes.

  • s: Socket - This file facilitates communication between the system and multiple processes simultaneously.file

  • s:- this file help to communicate the system to more than one process at once

System files

  • The directory structure in Linux provides a hierarchy for organizing files and directories. In this context, the "/dev" directory contains device files that represent physical or virtual devices connected to the system. These device files allow the system to communicate and interact with the devices.

  • On the other hand, the "/sys" directory is used by the system to view detailed information about devices and manage them. It provides a way to access and manipulate device-specific attributes and configurations.

File System(fs)

A file system is a method or structure used by an operating system to organize, store, and retrieve files on a storage device, such as a hard drive or solid-state drive (SSD). It provides a way for the operating system and user programs to interact with the stored data by providing a logical and consistent representation of files, directories, and their attributes.

Every filesystem has its on hirearchy, hirearchy is a way in which the file systemis been setup into the system

Hierarchy

  • /-> Root directory of the entire file system hierarchy or it is the primary hierarchy

  • bin -> Essential user command binaries are present here like ls, mkdir or other files which we use in cli and the binaries ehich we install from web

  • boot -> Bootloader files which the system uses in kernel are present here

  • dev ->Scripts which is used by the device drivers is present here

  • etc-> Contains config files

  • home -> Personal directory vary from user to user

  • lib-> The file which binaries might need to is present in this directory

  • media -> mount point for removable media. for example, when you insert a cd or pen drive into your system, a directory will automatically be created inside the media directory from where you can access its data

  • mnt -> temporary mounted file system

  • opt-> optional software packages

  • proc-> process info

  • root-> Home directory

  • run-> It keeps the information about about the system since then it was booted.

  • sbin-> system binaries only root can execute this

  • sys-> it is a virtual file system that provides information about the system's hardware devices and their corresponding drivers.

  • tmp-> storage for termporary files

  • usr-> user installed software and utilities

  • var-> variable directory. Whatever is continuously changing is stored in this directory

    Virtual file system

    Journaling

  • The journal acts as a transaction log, keeping track of the operations or changes that are about to be performed on the file system. Before the actual changes are made to the file system structures, they are first recorded in the journal.

  • When the system reboots after an unexpected shutdown or failure, it performs a file system check, commonly known as a "fsck" or "chkdsk" (check disk) operation. During this check, the file system examines the journal and applies any pending changes recorded in it. This helps to bring the file system back to a consistent state by ensuring that the interrupted operations are completed or rolled back, as needed.

Desktop file system types

  • ext4: ext4 is the latest and most widely used file system for Linux. It is the successor to ext3 and provides several improvements in terms of performance, scalability, and features. It supports large disk sizes of up to 1 exabyte and individual file sizes of up to 16 terabytes.

  • btrfs: btrfs (B-Tree File System) is a modern copy-on-write (COW) file system for Linux. It aims to provide advanced features such as snapshots, checksumming, and integrated RAID. It offers flexible and scalable storage management capabilities.

  • xfs: xfs is a high-performance journaling file system originally developed by Silicon Graphics, Inc. It is designed for scalability and efficient handling of large file systems. xfs is commonly used in environments where the system needs to manage file systems with a large size or high throughput requirements.

  • NTFS & FAT: NTFS (New Technology File System) and FAT (File Allocation Table) are file systems primarily used by Windows operating systems.

  • HFS+: HFS+ (Hierarchical File System Plus) is the file system used by Apple's macOS and Mac OS X. It supports features like journaling, file and folder metadata, and case-sensitive or case-insensitive file naming.

df -T

Use this command to check the file system on your system.

Partition

Instead of having one file system in your entire disk what you can do is create multiple partitions with different file systems. these partitions will act as multiple block devices.

There are two types of partition schemes

  • MBR(Master boot record) - It supports dsik size upto 2tb

  • it has a limit of 4 partitions-> Primary partitions

  • for primary partiton you can take only one partiton

  • GPT(GUID partition table) -

Filesystem structure

  • Filesystem is a organised collection of file and directories

  • Certainly! Here's the updated information with added punctuation:

    1. Boot Block: The boot block is located in the first few sectors of the file system. It is not used by the file system itself but is essential for booting the operating system. Every operating system requires a boot block to initiate the booting process.

    2. Super Block: The super block is a data block that follows the boot block. It contains vital information about the file system, such as the size of the inode table, the size of the operating system, the total number of data blocks, and other metadata related to the file system's structure and organization.

    3. Inode Table: The inode table is a database-like structure that manages files and directories within the file system. Each file or directory has a unique entry in the inode table.

    4. Data Blocks: Data blocks store the actual data of files and directories. These blocks hold the content of the files, including text, images, program code, or any other file data. The file system maintains a mapping between the inodes and the corresponding data blocks to retrieve and store file information accurately.

    sudo parted -l

To get the information about the partitions

cat /etc/fstab

It is a system configuration file that contains information about the available partitions and disks, and how they should be mounted by the system whenever the system is booted, as well as manually using the mount and unmount options.

Lets discuss the components of system configuration

  • File System: device or file that contains the file system(In my case it is showed by uuid instead of name)

  • Mount point: File system where the file is mounted

  • Type: The file system type(ext4,ntfs,vfat) it is showing vfat in my case which is windows and ext4 is for linux

  • Options: a comma-separated Pass fsck list of option that control how the file system is mounted

  • Dump: a flag indicating whether the file system should be included in the file system

  • Pass: a flag that indicating whether the file system should be checked for the errors by fsck(File system consistency check) utility when the system is booted

Swap: A swap partiton is a special type of file system that is used as a virtual memory by the Operating system when the system runs out of RAM it can use the swap partiton and use its memory and allows the system to run smoothly

To see the available and used disk space of the various file system in the linux

df -h

du -h "dir"

It will show you the disk usage in human-readable form according to the directory mentioned in the dir. If you don't specify a directory, it will show the disk usage of the directory you are currently in.

Inode(index node)

  • This is the database used to manage files it contains all the data such as name, size permission and location on the disk.

  • Sometimes, a problem arises when you have storage, but your system says that the storage is full. In this case, the issue is actually with the allocation of storage for inodes. The storage allocated for inodes is fully occupied, preventing the system from storing more data.

  • When a file is created, a new section is allocated in the inodes, which contains the specific ID of the file. However, as the number of files increases, the available inodes become filled up, even if there is still physical storage space available. Consequently, the system reports that the storage is full, even though there is remaining capacity.

      df -i
    

    This command is used to show the file system and inode number associated with it. Whenever a file is created, a new inode number is assigned to it. If a file is deleted and a new file is created at the same time, the system will assign the inode number of the deleted file to the new file.

ls -li

To show the the inode number of the file present in the current directory

How Inodes locate file

  • Whenever the user wants to access a specific file, first, the system searches the inodes. Inodes contain data about the file, including the location of the block on the disk that holds the file's data, its file size, and other metadata. By using the inodes, the system can quickly access the file instead of searching the entire system.

  • Symbolic links or soft links contains the reference of another file. So whenever you want to access this file it will redirects you to the file which is it being referenced

  • Soft links generally act as a shortcuts in windows. Inorder to make symlink you can use

ln -s "file path" "shortcut path"
  • In hardlinks the link directly points toward the inodes whereas the softlink points toward the file

  • if you delete the file the file which is linked with it will also become corrupted but in case of hardlink it won't become corrupted because the link is pointing to the inode you can still access the file by using that shortcut

ย