Understanding Linux File Links, Inodes, and Metadata
A deep dive into hard links vs. symbolic links, directory structures, and file metadata analysis.
Understanding Linux File Links, Inodes, and Metadata
Hard Links vs. Symbolic Links
Understanding how Linux links files is essential for efficient storage management and system configuration.
Hard Links
ln file1 file2: Creates a hard link. Both filenames point to the exact same inode on the disk.- Inode Consistency: If you edit one, the other changes. If you delete
file1, the data andfile2remain intact because the inode’s “link count” is still 1. - Limitation: Hard links cannot cross different filesystems or partitions.
Symbolic (Soft) Links
ln -s /usr/share/doc .: Creates a symbolic link (symlink) in the current directory pointing to the source path.- Pointer Mechanism: A symlink is a special file that simply contains the path to another file or directory.
- Broken Links: If the original file is moved or deleted, the symlink becomes “broken” or “dangling.”
pwd -P: Use this command to see the physical path of a directory instead of the logical path through a symlink.
Exploring File Metadata
Every file in Linux has metadata (data about data) that tracks permissions, ownership, and timestamps.
Analyzing with ls and stat
ls -li: Displays the inode number (the unique ID for the file on that filesystem) in the first column.stat /etc/services: Provides a comprehensive view of file metadata, including:- File Type: Regular file, directory, link, socket, or block device.
- Access/Modify/Change Times: Tracks when the file was last accessed, edited, or when its metadata changed.
- Link Count: Shows how many hard links point to that specific inode.
Special Directory Entries: . and ..
Every directory in Linux contains two hidden entries that facilitate navigation:
.(Dot): Points to the current directory itself...(Dot Dot): Points back to the parent directory.- The Link Count Rule: Every time you create a sub-directory (e.g.,
mkdir d1), the parent directory’s link count increases by 1 because the new sub-directory’s..entry points back to it.
Common Linux File Types
When looking at the first character of an ls -l output, you will encounter these types:
| Character | File Type |
|---|---|
| - | Regular file |
| d | Directory |
| l | Symbolic link |
| p | Named pipe |
| s | Socket |
| b / c | Block or Character device |
This post is licensed under CC BY 4.0 by the author.
