Skip to content

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.

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.

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.

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 file
  • d : Directory
  • l : Symbolic link
  • c : 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

To truly understand Linux file management, you must understand how data is actually stored on the disk.

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.

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!

Every file has metadata stored in its Inode (permissions, ownership, size, etc.).

Linux tracks three main timestamps for files:

  • mtime (Modify Time): When the file’s contents were last changed. (This is what ls -l shows).
  • 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.

To view the complete metadata and all three timestamps of a file, use the stat command:

Terminal window
stat /etc/passwd

Because filenames just point to Inodes, we can create multiple links to the same data.

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.
Terminal window
# Create a hard link
ln original.txt hardlink.txt
# Check the Inode number (the -i flag) to see they are identical
ls -li original.txt hardlink.txt

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.
Terminal window
# Create a symbolic link (-s flag)
ln -s /var/log/nginx/access.log web_logs
# Use readlink to see where a symlink actually points
readlink web_logs
# Output: /var/log/nginx/access.log