Linux Filesystem

A Linux file system is a structured collection of files stored on a disk drive or partition. A partition is a section of memory that contains specific data, and a machine can have multiple partitions. Each partition typically contains its own file system.

The Linux file system is an essential part of the Linux operating system that manages data storage. It organizes files on disk storage and manages file names, sizes, creation dates, and other file-related information.

The Linux file system follows a hierarchical file structure that consists of a root directory and its subdirectories. All other directories can be accessed from the root directory. Normally, a partition has only one file system, but it may contain more than one file system. It has a root directory that contains other files and directories.


Note: Windows users usually use the word folder, however, in Linux terminology, folders are referred to as directories.

Pathnames

A pathname is essentially a sequence of names that form a path through the file system hierarchy. It determines how to navigate through the directory names to reach a specific object.

The pathname text string consists of the directories that need to be traversed in order to arrive at the intended destination. Each directory name is separated by forward slashes within the text string. For instance, /etc/passwd or /usr/bin/wc. This means that directory and file names cannot contain slashes themselves, as these are used to separate the names in the pathname text string.

Therefore, slashes serve as separators in the pathname text string.

The difference between an absolute and a relative path is that an absolute path specifies the location from the root directory, whereas a relative path is related to the current directory.

File Types in linux



1. Ext, Ext2, Ext3 and Ext4 file system


The file system Ext stands for Extended File System. It was primarily developed for MINIX OS. The Ext file system is an older version and is no longer used due to some limitations.

Ext2 is the first Linux file system that allows managing two terabytes of data. Ext3 is developed through Ext2; it is an upgraded version of Ext2 and contains backward compatibility. The major drawback of Ext3 is that it does not support servers because this file system does not support file recovery and disk snapshots.

The ext4 file system is the faster file system among all the Ext file systems. It is a very compatible option for the SSD (solid-state drive) disks, and it is the default file system in Linux distribution.

2. JFS File System

JFS stands for Journaled File System, and it is developed by IBM for AIX Unix. It is an alternative to the Ext file system. It can also be used in place of Ext4, where stability is needed with few resources. It is a handy file system when CPU power is limited.

3. ReiserFS File System

ReiserFS is an alternative to the Ext3 file system. It has improved performance and advanced features. In the earlier time, the ReiserFS was used as the default file system in SUSE Linux, but later it has changed some policies, so SUSE returned to Ext3. This file system dynamically supports the file extension, but it has some drawbacks in performance.

4. XFS File System

XFS file system was considered as high-speed JFS, which is developed for parallel I/O processing. NASA still using this file system with its high storage server (300+ Terabyte server).

5. Btrfs File System

Btrfs stands for the B tree file system. It is used for fault tolerance, repair systems, fun administration, extensive storage configuration, and more. It is not a good suit for the production system.

6. Swap File System

The swap file system is used for memory paging in the Linux operating system during the system hibernation. A system that never goes in a hibernate state is required to have swap space equal to its RAM size.

In a Linux system, everything is either a file or a process. Most file system implementations recognize seven types of files.

Even when developers add something new and wonderful to the file tree (such as the process information under / it must still be made to look like one of these seven types:
  • Regular files
  • Directories
  • Character device files
  • Block device files
  • Local domain sockets
  • Named pipes (FIFO)
  • Link files

Regular or Ordinary files

Regular or ordinary files are used to store data of various content types, such as text, audio, video, images, scripts, and programs. They are the most common type of file found on UNIX and Linux operating systems.

In Linux, regular files can be created with or without an extension. An extension is a period followed by a series of characters that indicates the type of file. For example, a file with a .txt extension is a text file.

Regular files are represented by a hyphen (-) in the first character of the file's permission mode. The permission mode specifies who can read, write, or execute the file.

Directories

In a computer system, a directory is a binary file that contains named references to other files. Its primary purpose is to track and locate other files and directories. 

You can create directories using the mkdir command and delete them with the rmdir command, but only if they are empty. Directories are used to organize files in a hierarchy. 

In the Linux file system, the root directory is denoted as '/', and all files and directories are created under this directory. Each directory, except the root directory, has a parent directory.

Special Files: Character device & Block device file

In Linux, all hardware devices, including hard drives, printers, monitors, terminal emulators, and CD/DVD drives are treated as special files. The purpose of these special files is to expose the hardware device as a file in the file system. This provides a universal interface for hardware devices, including virtual devices created and used by the kernel. The benefit of this is that tools for file I/O can access these devices. When data is read from or written to a special file, the operation happens immediately and is not subject to conventional filesystem rules.

Link Files: Hard link & Soft link files

Link files are used to refer to a file with a different filename or from a different location. They are essentially pointers to another file. There are two types of links: hard links and symbolic or soft links.

A hard link creates a mirror copy of the original file. However, it cannot be created to a directory or a file on another filesystem.

On the other hand, a symbolic or "soft" link points to a file by its name. It can be created to a directory or a file on another filesystem.

Socket Files

Applications use sockets as communication endpoints to exchange data with each other. For instance, if an application on a local system wants to communicate with an application on a remote system, it connects to the socket of that application using the associated IP address and port number.

Sockets are also used by applications that provide services to other applications or remote clients to accept connections. Each socket has an associated IP address and port number, which enable it to accept connections from clients.

Linux uses socket files to simplify communication between local applications. Instead of relying on networking and sockets, socket files allow local applications to exchange data easily. 

These files are unique in that they use a file name as their address, instead of an IP address and port number. This makes it simpler for applications on the same system to communicate with each other. 

Inter-process communication is enabled through the use of the sendmsg() and recvmsg() system calls, which are specifically designed to facilitate communication between local applications.

Named Pipes

Named pipes, also known as "FIFO files", are a way for two processes to communicate on the same host, just like local domain sockets. You can create named pipes using mknod and remove them using rm. It is worth noting that both named pipes and local domain sockets serve similar purposes, and their existence is essentially a result of historical reasons. If UNIX and Linux were designed from scratch today, it is unlikely that either of them would exist, as network sockets would likely replace both.