05. Files & Inodes
In Linux, there is a fundamental philosophy: “Everything is a file.” Whether it’s a text document, a directory, a hard drive, a keyboard, or a network connection, Linux represents it as a file.
05.1 The Concept of Files and Directories
Section titled “05.1 The Concept of Files and Directories”A file is simply a one-dimensional array of bytes. The Linux kernel does not care if the file is a text file, a compiled binary, or an image.
Directories
Section titled “Directories”A directory is also just a file! However, it is a special type of file that contains a list of filenames and their corresponding Inodes.
File Types
Section titled “File Types”You can determine the true type of a file using the file command (since Linux does not rely on file extensions like .txt or .exe).
When you run ls -l, the very first character of the output tells you the file type:
-: Regular filed: Directoryl: Symbolic linkc: Character device (e.g., a keyboard,/dev/tty)b: Block device (e.g., a hard drive,/dev/sda)s: Socket (used for inter-process networking)p: Named pipe
05.2 The Inode Architecture
Section titled “05.2 The Inode Architecture”To truly understand Linux file management, you must understand how data is actually stored on the disk.
What is an Inode?
Section titled “What is an Inode?”An Inode (Index Node) is a data structure on the filesystem that stores the metadata about a file and points to the physical data blocks on the hard drive where the file’s content actually lives.
Crucially, the Inode does NOT store the filename. The filename is simply an entry in a directory that points to an Inode.
The Storage Flow
Section titled “The Storage Flow”This is the most important concept in Linux storage:
Filename (in a directory) ↓ Inode (Metadata & Pointers) ↓ Data Blocks (The actual physical 1s and 0s)Because of this architecture, multiple filenames can point to the same exact Inode!
05.3 File Metadata & Timestamps
Section titled “05.3 File Metadata & Timestamps”Every file has metadata stored in its Inode (permissions, ownership, size, etc.).
Timestamps
Section titled “Timestamps”Linux tracks three main timestamps for files:
- mtime (Modify Time): When the file’s contents were last changed. (This is what
ls -lshows). - ctime (Change Time): When the file’s metadata (Inode) was last changed (e.g., changing permissions with
chmod). - atime (Access Time): When the file was last read.
The stat Command
Section titled “The stat Command”To view the complete metadata and all three timestamps of a file, use the stat command:
stat /etc/passwd05.4 Linking (Hard vs Symbolic)
Section titled “05.4 Linking (Hard vs Symbolic)”Because filenames just point to Inodes, we can create multiple links to the same data.
Hard Links
Section titled “Hard Links”A Hard Link is a new filename that points to the exact same Inode as the original file.
- They share the exact same data blocks.
- If you delete the original file, the data is not lost as long as the Hard Link exists. The data is only truly deleted when the “link count” on the Inode drops to
0. - Limitation: Hard links cannot cross different filesystems/partitions, and you cannot hard link a directory.
# Create a hard linkln original.txt hardlink.txt
# Check the Inode number (the -i flag) to see they are identicalls -li original.txt hardlink.txtSymbolic Links (Soft Links)
Section titled “Symbolic Links (Soft Links)”A Symbolic Link is completely different. It is a brand new file (with its own Inode) that simply contains the text path to the original file. It acts like a “Shortcut” in Windows.
- If you delete the original file, the Symbolic Link breaks (becomes a “dangling link”).
- They can cross filesystems and can link to directories.
# Create a symbolic link (-s flag)ln -s /var/log/nginx/access.log web_logs
# Use readlink to see where a symlink actually pointsreadlink web_logs# Output: /var/log/nginx/access.log