LAB-FS-06 - Inodes, Links & File Internals
Inodes, Links & File Internals
Understand the hidden architecture of files (Inodes) and master the creation of hard links and symbolic (soft) links to manipulate the filesystem.
- Understand the hidden architecture of files (Inodes) and master the creation of hard links and symbolic (soft) links to manipulate the filesystem.
- Repeat the workflow without copy-paste or step-by-step prompting.
- Use a disposable practice directory so accidental edits do not touch personal files.
Part A: The Field Guide
🎯 What & Why
You think a file is a name and the data inside it.
Linux disagrees. In Linux, a file has two completely separate parts:
- The actual 1s and 0s of data scattered across the hard drive platter.
- The index card (called an Inode) that tracks where those 1s and 0s are, who owns them, what the permissions are, and how big the data is.
Notice what is missing from the Inode? The filename.
The filename is NOT part of the file. A filename is just a sticky note attached to a directory pointing to an Inode.
Understanding this split reality (Data/Inode vs Filename) unlocks the power of Links. You can attach three different filenames to the exact same Inode. You can create shortcuts that span across different hard drives.
🧠 Mental Model: The Library
Think of a massive library.
- The Book (Data): The actual bounded paper sitting on Shelf 4, Row 12.
- The Inode: The unique serial number (e.g.,
#9942) stamped on the book, tracking its weight, author, and checkout permissions. - The Directory Entry (Filename): A card in the front lobby catalog that says: “The Great Gatsby is Book
#9942.”
Hard Links
A Hard Link is writing a second card in the lobby catalog that says: “My Awesome Novel is Book #9942.”
Now there are two totally different names (“The Great Gatsby” and “My Awesome Novel”). Both point directly to the exact same physical book #9942. If you edit one, the other immediately reflects the change, because they are the same book. If you delete one catalog card, the book remains safe, because the other card still points to it. The book is only destroyed when ALL catalog cards pointing to 9942 are destroyed.
Symbolic (Soft) Links
A Soft Link is a card in the lobby catalog that says: “For an awesome novel, go look at the card for ‘The Great Gatsby’.”
It doesn’t point to the book (Inode #9942). It points to the name (The Great Gatsby). If someone deletes the “Great Gatsby” card, your Soft Link card becomes useless — a “broken link.”
📖 Command Reference
Investigating Inodes: ls -i
The -i flag shows the unique Inode serial number of a file.
$ ls -i report.txt 3459142 report.txt
3459142 is the actual file. “report.txt” is just the sticky note pointing to it.
Creating Hard Links: ln
Syntax: ln [SOURCE_TARGET] [NEW_LINK_NAME]
$ touch original_file.txt $ ls -i original_file.txt 12345 original_file.txt
$ # Create a hard link $ ln original_file.txt secondary_name.txt
$ # Check the inodes $ ls -i *_file.txt 12345 original_file.txt 12345 secondary_name.txt
Notice the Inode numbers are identical. They are the same file.
Creating Symbolic (Soft) Links: ln -s
Syntax: ln -s [SOURCE_TARGET] [NEW_LINK_NAME]
$ # Create a soft link $ ln -s original_file.txt shortcut_name.txt
$ ls -li 12345 -rw-r—r— 2 alice users 10 original_file.txt 88899 lrwxrwxrwx 1 alice users 17 shortcut_name.txt -> original_file.txt
Notice the Soft Link has a different Inode (88899). It is a separate file entirely. Its only job is to point the kernel toward the name original_file.txt. The l at the far left of the permissions string denotes a symbolic link.
🛑 The Hard Link Limitation
Hard Links CANNOT cross file systems. You cannot hard link a file on your / drive to your /backups USB drive, because Inode numbers are only unique per hard drive.
Soft links CAN cross file systems. Because they just point to a text path (e.g., /var/log/syslog), they don’t care what physical drive the target is on.
🌍 Real Scenarios
Scenario 1: Version Control Shortcuts (Soft Links)
You have software installed in /opt/node-v20.1.0/bin/node.
You don’t want to type that. You create a soft link:
sudo ln -s /opt/node-v20.1.0/bin/node /usr/bin/node
When Node v21 comes out, you don’t break everything; you just delete the soft link and recreate it pointing to the new /opt/node-v21.0.0... directory. All scripts calling node keep working seamlessly.
Scenario 2: The “rm” Safety Net (Hard Links)
You have a critical database file.
You create a hard link to it in a hidden backup folder.
If a junior admin accidentally runs rm critical_database.db, the data is completely safe. Why? Because rm only deletes the name (the sticky note). The data remains intact on disk because your hidden hard link is still pointing to that Inode!
Scenario 3: Exposing Web Folders (Soft Links)
Your web server serves files from /var/www/html. Your development team works in /home/devteam/webapp.
Instead of moving files constantly, you create a soft link inside /var/www pointing to /home/devteam/webapp. The web server seamlessly follows the link and serves the developers’ live files.
⚠️ Gotchas & Pitfalls
- Directories and Hard Links — Linux strictly forbids creating Hard Links pointing to Directories. It would create infinite loops in the file system tree (
.and..are the only exceptions, generated by the kernel). You MUST use Soft Links (ln -s) for directories. - Broken Symlinks — If you
mvorrmthe original source file, the Soft Link turns red in your terminal. It still exists, but pointing to the void. - The
rm -rf /exception — Symlinks protect the source fromrm. But if you recursively delete a directory usingrm -rf, and it encounters a symbolic link to another directory,rmis smart enough to just delete the link itself, NOT follow it and delete the target’s contents.
Part B: The Drill Deck
Terminal Required: Open your Linux terminal for these exercises.
G Guided Step by step - type exactly this and compare the result >
Exercise G1: The Inode Reveal
- Open your terminal in your home directory (
cd ~). - Create an empty test file:
touch source.txt - Check its Inode number:
ls -li source.txt(The-iflag is key here). - Look at the number on the far left. Write it down mentally.
Exercise G2: Forging a Hard Link
- Let’s create a hard link:
ln source.txt hardlink.txt - Run your check again:
ls -li *.txt - Notice two things:
- The Inode numbers are IDENTICAL.
- The link count (the number right after the permissions
rw-r--r--) is now2. The system knows there are 2 names pointing to this 1 data block!
Exercise G3: Forging a Soft Link
- Let’s create a soft link:
ln -s source.txt softlink.txt - Run your check:
ls -li *.txt - Notice three things about the soft link:
- It has a completely DIFFERENT Inode number.
- The line explicitly shows the arrow:
softlink.txt -> source.txt - The permissions start with
l(lower case L).
S Solo Task described, hints available - figure it out >
Exercise S1: The Data Survival Test
- Edit the hard link file.
echo "SURVIVAL DATA" > hardlink.txt - Read the source file.
cat source.txt. The data is there! (They are the same Inode). - Now, delete the original source file:
rm source.txt - Read the hard link:
cat hardlink.txt. The data survives! You only deleted one of the names. - Check the soft link:
cat softlink.txt. It fails with “No such file or directory”! The soft link is broken because the name it was pointing to (source.txt) is gone.
Exercise S2: Symlinking a Directory
- Create a directory:
mkdir target_dir - Try to hard link it:
ln target_dir hard_dir. It will fail with “hard link not allowed for directory.” - Soft link it:
ln -s target_dir soft_dir - Use
ls -lto verifysoft_dirpoints totarget_dir.
M Mission Real scenario - no hints, combine multiple skills >
Mission M1: The Out-of-Space Illusion
You run df -h and your hard drive says it has 50 GB of free space.
You try to create a tiny 1-kilobyte text file: touch new_file.txt.
Linux immediately errors: “No space left on device.”
How is this possible?
Hint: Remember that Linux separates Data (blocks) and Index cards (Inodes). Every time you format a drive, it pre-creates a fixed number of Inodes. Formulate a search query (or ask an AI/DuckDuckGo) for “Linux out of inodes”.
What command would you run to check your Inode usage percentage? (df -?)
Mission M2: The System Linking Trace
System administrators use symlinks to manage configurations seamlessly.
- Navigate to
/etc. - Run
ls -l | grep "^l"(List long format, pipe to grep to only show lines starting with ‘l’ for links). - Look at how many files in
/etcare actually just arrows pointing somewhere else! - The timezone configuration is famously a symlink. Find where your
/etc/localtimefile points to.
You now understand the hidden architecture of the Linux file system!