Links and Inodes
How Linux actually stores files. Inodes, directory entries and link counts, hard links versus symbolic links, how deployments switch releases with a symlink, broken links, running out of inodes, and why deleting a file doesn't always free disk space.
A file's name is not the file
When you create /home/aditya/notes.txt, Linux stores two separate things:
- An inode (index node): a small record with a number that holds everything about the file: its size, owner, group, permissions, timestamps, link count, and the location of its data blocks on disk.
- A directory entry: inside the folder
/home/aditya, a line that says "the namenotes.txtmeans inode number 1474720".
A directory is really just a table of names → inode numbers. The inode doesn't know its own name.
How a file is stored
month,revenue ...Two names point to inode 1474720: they are the same file. The link count says how many names it has.
This separation explains renaming (only the directory entry changes, which is why mv within a disk is instant), hard links, and some surprising disk-space problems.
Seeing inodes
ls -li /srv/dataWhat -i does: adds the inode number as the first column.
total 12
1474720 -rw-r--r-- 2 root root 36 Nov 24 15:23 latest-report.csv
1474721 -rw-r--r-- 1 root root 36 Nov 24 15:23 report-q3.csv
1474720 -rw-r--r-- 2 root root 36 Nov 24 15:23 report-q4.csv- Column 1: the inode number.
latest-report.csvandreport-q4.csvshare1474720. - Column 3 (the one you skipped in earlier lessons): the link count, how many names point to the inode. It's
2for the shared one and1forreport-q3.csv.
stat shows the same information, plus everything else in the inode:
stat /srv/data/report-q4.csv File: /srv/data/report-q4.csv
Size: 36 Blocks: 8 IO Block: 4096 regular file
Device: 0,41 Inode: 1474720 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2026-11-24 15:23:55.198130413 +0530
Modify: 2026-11-24 15:23:55.203914746 +0530
Change: 2026-11-24 15:23:55.203914746 +0530
Birth: 2026-11-24 15:23:55.198130413 +0530Inode: 1474720 and Links: 2. Notice that File: shows the name you asked about, not a name stored in the inode. Inode numbers are unique within one filesystem (the Device line); two disks can each have an inode 1474720.
Hard links: another name for the same file
A hard link is simply an extra directory entry pointing to an existing inode. Both names are equally "real": there is no original and no copy.
echo "test" > /root/a.txt
ln /root/a.txt /root/b.txt
ls -li /root/a.txt /root/b.txt| Part | Meaning |
|---|---|
ln | make a link. Without -s, it's a hard link. |
/root/a.txt | the existing file (the target) |
/root/b.txt | the new name |
1474747 -rw-r--r-- 2 root root 5 Nov 24 15:23 /root/a.txt
1474747 -rw-r--r-- 2 root root 5 Nov 24 15:23 /root/b.txtSame inode, link count 2. Edit one and the other "changes" too, because there's only one file.
Now delete one name:
rm /root/a.txt
ls -li /root/b.txt
cat /root/b.txt1474747 -rw-r--r-- 1 root root 5 Nov 24 15:23 /root/b.txt
testThe link count dropped to 1, and the data is still there. That's what rm really does: it removes a name and decreases the link count. The inode and its data are freed only when the count reaches 0 (and no program has the file open).
Hard links have two limits:
$ ln /srv/data /root/datalink
ln: /srv/data: hard link not allowed for directory- No hard links to directories (it would create loops in the tree).
- Same filesystem only, because inode numbers only mean something within one filesystem.
Where you meet them: backup tools like rsync --link-dest and Time Machine use hard links to store unchanged files once across many daily snapshots. And every directory has a link count of at least 2: its own name, plus the . entry inside it.
Inodes and hard links
Find the two report files that are really the same file.
ls -i shows each file's inode number, and the second column of ls -l shows the link count: how many names point to that inode. Two names with the same inode number are hard links: the same file, with the same data, reachable under two names. Change one and you change "both", because there is only one.
1. Show inode numbers
ls -li /srv/data| Part | Meaning |
|---|---|
-l | long format |
-i | add the inode number as the first column |
total 12
1474720 -rw-r--r-- 2 root root 36 Nov 24 15:23 latest-report.csv
1474721 -rw-r--r-- 1 root root 36 Nov 24 15:23 report-q3.csv
1474720 -rw-r--r-- 2 root root 36 Nov 24 15:23 report-q4.csv- The first column is the inode number. Two files share
1474720(your numbers will differ). - The third column, the link count, is
2for both: that inode has two names. report-q3.csvhas its own inode and a link count of1: a normal, separate file.
2. Confirm with stat
stat /srv/data/report-q4.csv File: /srv/data/report-q4.csv
Size: 36 Blocks: 8 IO Block: 4096 regular file
Device: 0,41 Inode: 1474720 Links: 2
...Inode: 1474720 and Links: 2. To find every name of an inode: find /srv -inum 1474720 (with your number).
3. Make a hard link yourself
echo "test" > /root/a.txt
ln /root/a.txt /root/b.txt
ls -li /root/a.txt /root/b.txt
rm /root/a.txt
cat /root/b.txt| Command | What it does |
|---|---|
ln /root/a.txt /root/b.txt | adds a second name, b.txt, for the same inode. No -s: a hard link. |
rm /root/a.txt | removes one name. The data stays because another name still points to it. |
1474747 -rw-r--r-- 2 root root 5 Nov 24 15:23 /root/a.txt
1474747 -rw-r--r-- 2 root root 5 Nov 24 15:23 /root/b.txt
testSame inode, link count 2. After rm a.txt, b.txt still holds test. The data is only freed when the last name is removed (and no program has the file open, as you'll see in step 3).
In /srv/data, find the two file names that are hard links to the same file. Write both names (just the names, one per line) to /root/same-file.txt.
Expected result: /root/same-file.txt contains the two names that share an inode number.
Hint: the commands, explained
Compare the first column of ls -li /srv/data, then for example:
printf 'one.csv\ntwo.csv\n' > /root/same-file.txtWhat each part does
ls -lishowed which file names share an inode number (the first column). Those are hard links to the same data.printf 'one.csv\ntwo.csv\n'writes the two names on separate lines.
Symbolic links: a signpost to a path
A symbolic link (symlink, soft link) is a small separate file whose content is a path. When a program opens it, the kernel follows the path to the real file. Think of it as a shortcut on a Windows desktop.
Hard link versus symbolic link
ln data.csv backup.csvln -s /srv/data.csv shortcut.csvln -s /etc/hosts /root/hosts-link
ls -li /etc/hosts /root/hosts-link| Part | Meaning |
|---|---|
ln -s | create a symbolic link |
/etc/hosts | the target: the path the link will contain |
/root/hosts-link | the link's name |
1474662 -rw-r--r-- 1 root root 20 Nov 24 15:23 /etc/hosts
1474731 lrwxrwxrwx 1 root root 10 Nov 24 15:23 /root/hosts-link -> /etc/hosts- The link has its own inode (
1474731): it's a separate small file. - The type is
l, and->shows where it points. - Its size,
10, is the length of the path it stores (/etc/hostshas 10 characters). - Its permissions always show
rwxrwxrwx. They don't matter: access is decided by the target's permissions.
Hard link vs symlink at a glance
| Hard link | Symbolic link | |
|---|---|---|
| Command | ln TARGET NAME | ln -s TARGET NAME |
| What it is | another name for the same inode | a file containing a path |
| Own inode? | no, shares it | yes |
| Target deleted | still works | breaks ("dangling") |
| Directories | not allowed | allowed |
| Across filesystems | not allowed | allowed |
In ls -l | looks like a normal file, link count 2+ | type l, shows -> target |
| Used for | backups, snapshots | shortcuts, releases, config switching (much more common) |
Reading links
| Command | Shows |
|---|---|
ls -l LINK | the link and its target |
readlink LINK | the stored path, exactly as written |
readlink -f LINK | the final absolute path, following every link in the chain |
Relative or absolute targets?
A symlink stores exactly the text you give it. A relative target is resolved from the folder the link is in, not from where you were when you created it:
cd /srv/app
ln -s releases/v1.4 current # current -> releases/v1.4 (relative)
ln -s /srv/app/releases/v1.4 current-abs # absoluteRelative links keep working if you move the whole folder tree (for example /srv/app to /opt/app). Absolute links keep working if you move the link itself.
Symlinks in real DevOps work
Symlinks are everywhere on servers once you notice them:
| Where | What the link does |
|---|---|
/srv/app/current -> releases/v1.5 | zero-downtime releases: each version in its own folder, current points to the live one. Deploy or roll back by moving the link. |
/etc/nginx/sites-enabled/shop -> ../sites-available/shop | enable an nginx site by linking it, disable by removing the link |
/etc/systemd/system/multi-user.target.wants/nginx.service | how systemctl enable works (you'll see it in the systemd lessons) |
/usr/bin/python3 -> python3.12 | pick which version a generic name runs (update-alternatives manages these) |
/etc/localtime -> /usr/share/zoneinfo/Asia/Kolkata | the server's time zone |
/bin -> usr/bin | compatibility after /usr merge |
Switching a release safely
ln -sfn releases/v1.5 /srv/app/current| Option | Why it's needed |
|---|---|
-s | symbolic link |
-f | replace the existing current link |
-n | if current is a link to a folder, replace the link itself instead of creating a new link inside that folder |
lrwxrwxrwx 1 root root 13 Nov 24 15:23 /srv/app/current -> releases/v1.5No files were copied. Every program that reads from /srv/app/current now gets version 1.5. Rolling back is the same command with v1.4.
Forgetting -n when updating a link to a folder: ln -sf releases/v1.5 current follows current into releases/v1.4 and creates releases/v1.4/v1.5 there, while current still points to the old release. Always use ln -sfn to replace folder links.
Broken links
When the target disappears, the link stays and points at nothing:
$ ls -l /srv/app/logs
lrwxrwxrwx 1 root root 15 Nov 24 15:23 /srv/app/logs -> /var/log/oldapp
$ ls /srv/app/logs/
ls: cannot access '/srv/app/logs/': No such file or directoryFind all of them with:
find /srv -xtype lDelete a link with rm LINK. This removes only the link, never its target. Don't add a trailing slash to a link that points to a folder: rm -r current/ would delete the contents of the folder it points to.
Switch a release with a symlink
Point the app's current link at release v1.5 and create a shortcut to its config.
A symbolic link (symlink) is a tiny file that just contains a path. When you open it, Linux follows the path to the real file or folder. Deployment tools (Capistrano, Ansible, many CI pipelines) use this for zero-copy releases: each version lives in its own folder, and a link called current points at the live one. Releasing, or rolling back, is just changing where current points.
1. Look at the app's layout
ls -l /srv/app
cat /srv/app/current/VERSIONtotal 8
lrwxrwxrwx 1 root root 13 Nov 24 15:23 current -> releases/v1.4
lrwxrwxrwx 1 root root 15 Nov 24 15:23 logs -> /var/log/oldapp
drwxr-xr-x 4 root root 4096 Nov 24 15:23 releases
drwxr-xr-x 2 root root 4096 Nov 24 15:23 sharedversion=1.4- Lines starting with
lare symlinks, and->shows where each points. current -> releases/v1.4: the live release is 1.4. Anything that uses/srv/app/current(a web server, a systemd service) runs 1.4.- Symlink permissions always show
rwxrwxrwx; what counts are the target's permissions.
2. Read where a link points
readlink /srv/app/current
readlink -f /srv/app/current| Command | Prints |
|---|---|
readlink LINK | the path stored in the link, exactly as written |
readlink -f LINK | the full, final absolute path after following every link |
releases/v1.4
/srv/app/releases/v1.4The link stores a relative path (releases/v1.4), relative to the folder the link lives in. readlink -f resolves it to the absolute path.
3. Switch the release
ln -sfn releases/v1.5 /srv/app/current
ls -l /srv/app/current
cat /srv/app/current/VERSION| Part | Meaning |
|---|---|
ln -s | create a symbolic link |
-f | force: replace the existing link |
-n | treat an existing link to a folder as a file, so it gets replaced instead of creating a new link inside the folder it points to |
releases/v1.5 | the target: what the link should point to |
/srv/app/current | the link's name |
lrwxrwxrwx 1 root root 13 Nov 24 15:23 /srv/app/current -> releases/v1.5
version=1.5Released. Nothing was copied, and rolling back is the same command with v1.4.
The argument order is the same as cp: target first, link name second: ln -s WHAT-IT-POINTS-TO NAME-OF-LINK. Getting it backwards is the most common symlink mistake. And forgetting -n when replacing a link to a folder creates a link inside the old release folder instead of switching it.
- Switch
/srv/app/currentso it points toreleases/v1.5. - Create a symlink
/root/config-link.yamlpointing to/srv/app/shared/config.yaml.
Expected result: cat /srv/app/current/VERSION prints version=1.5, and cat /root/config-link.yaml shows the config.
Hint: the commands, explained
ln -sfn releases/v1.5 /srv/app/current
ln -s /srv/app/shared/config.yaml /root/config-link.yamlWhat each part does
ln -s TARGET LINK: create a symbolic link (a pointer to the target path).-freplace the link if it already exists,-ntreat an existing link to a folder as a file to replace (otherwise ln would create a link inside the old folder).- Pointing
currentatreleases/v1.5is how many deployments switch versions in one step.
Running out of inodes
Every file and folder needs one inode, and a filesystem gets a fixed number of inodes when it's created. If something creates millions of tiny files (session files, cache entries, mail queues, a runaway script), you can run out of inodes while plenty of bytes are still free. The error is the same as a full disk: No space left on device.
df -i /What -i does: shows inode usage instead of byte usage.
Filesystem Inodes IUsed IFree IUse% Mounted on
overlay 16777216 456573 16320643 3% /16.7 million inodes, 3% used. If IUse% is near 100% while df -h shows free space, find the folder with the most files (find /var -xdev -type f | cut -d/ -f2,3 | sort | uniq -c | sort -n or simply du --inodes) and clean it up.
Deleted, but still using space
This one comes up in real incidents and in interviews. A log file grows to 30 GB, someone runs rm on it, and... df still shows the disk full.
Why: rm removed the name, but the application still has the file open. As long as any process holds it open, the inode and its data stay on disk. du, which adds up files by name, can't see it anymore. df, which asks the filesystem, still counts it.
du -sh /var/log/shopapp
lsof +L1| Command | What it does |
|---|---|
du -sh /var/log/shopapp | total size of the named files in the folder |
lsof +L1 | list open files whose link count is below 1: deleted but still open |
4.0K /var/log/shopappCOMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
shop-work 28 root 3w REG 0,41 31457280 0 1474740 /var/log/shopapp/debug.log (deleted)| Column | Meaning |
|---|---|
COMMAND, PID | the process holding the file: shop-worker, PID 28 |
FD | file descriptor 3, open for writing |
SIZE/OFF | 31,457,280 bytes: 30 MB still on disk |
NLINK | 0: no names left |
NAME | the old path, marked (deleted) |
Every process also lists its open files in /proc/PID/fd:
$ ls -l /proc/28/fd
l-wx------ 1 root root 64 Nov 24 15:24 3 -> /var/log/shopapp/debug.log (deleted)The fix: make the process close the file. Restart the service, or send the signal it uses to reopen logs (many daemons reopen on SIGHUP or SIGUSR1). The prevention: never rm a log file that's in use. Empty it instead, which keeps the inode and frees the space instantly:
truncate -s 0 /var/log/app/debug.log # or: > /var/log/app/debug.logThis is also exactly why logrotate has a copytruncate option.
"What is an inode?" The data structure that stores a file's metadata (owner, permissions, size, timestamps, link count) and the location of its data. Names live in directories and point to inode numbers.
"Hard link vs soft link?" A hard link is another name for the same inode; it survives deleting the original but can't point to directories or other filesystems. A symlink is a separate file containing a path; it can point anywhere but breaks if the target is removed.
"The disk is full but du shows nothing big. Why?" A deleted file is still held open by a process. lsof +L1 finds it; restarting the process frees the space.
"No space left on device, but df -h shows free space?" Out of inodes. Check df -i.
Broken links and deleted files
Remove the broken link and find the process holding a deleted log file.
Two problems that inodes explain, and that you will meet on real servers:
- A broken (dangling) symlink points to a path that no longer exists. It still shows up in
ls, but opening it fails. - "The disk is full, but I deleted the big log!" Deleting a file removes its name. If a program still has the file open, the inode and its data stay on disk until the program closes it.
ducan't see it (no name), butdfstill counts it.
1. Find broken symlinks
ls -l /srv/app/logs
ls /srv/app/logs/
find /srv -xtype l| Command | What it does |
|---|---|
ls -l /srv/app/logs | shows the link and where it points |
ls /srv/app/logs/ | tries to open it: fails if the target is missing |
find /srv -xtype l | finds links whose target doesn't exist (-xtype l: "still a link after following it", which only happens when it's broken) |
lrwxrwxrwx 1 root root 15 Nov 24 15:23 /srv/app/logs -> /var/log/oldapp
ls: cannot access '/srv/app/logs/': No such file or directory
/srv/app/logsThe link exists, but /var/log/oldapp doesn't. Remove it with rm /srv/app/logs. rm on a symlink removes only the link, never the target. (Careful: write it without a trailing slash. rm -r /srv/app/logs/ on a working link would delete the contents of the target folder.)
2. Space held by a deleted file
The shop's worker process wrote a 30 MB debug log, and someone deleted the file to free space. But the worker is still running:
du -sh /var/log/shopapp
lsof +L1| Command | What it does |
|---|---|
du -sh /var/log/shopapp | adds up the sizes of the files that have names in this folder |
lsof +L1 | list open files with a link count below 1: files that have been deleted but are still open |
4.0K /var/log/shopappCOMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
shop-work 28 root 3w REG 0,41 31457280 0 1474740 /var/log/shopapp/debug.log (deleted)dusays the folder holds almost nothing.lsoffinds the truth: process 28 (shop-worker) still hasdebug.logopen for writing (3w= file descriptor 3, write).SIZE/OFFis 31,457,280 bytes (30 MB),NLINKis0(no names left) and the name ends in(deleted).
You can see the same thing in /proc, where every process lists its open files:
ls -l /proc/$(pgrep -x shop-worker)/fdl-wx------ 1 root root 64 Nov 24 15:24 3 -> /var/log/shopapp/debug.log (deleted)The fix is to make the process close the file: restart the service (systemctl restart ...) or, if it supports it, ask it to reopen its logs. On a real server, the right way to empty a log that's in use is to truncate it instead of deleting it: truncate -s 0 app.log or > app.log keeps the name and frees the space immediately.
- Remove the broken symlink
/srv/app/logs. - Find the PID of the process that is holding the deleted
debug.logopen, and save it to/root/worker-pid.txt.
Expected result: find /srv -xtype l prints nothing, and /root/worker-pid.txt contains the right PID.
Hint: the commands, explained
rm /srv/app/logs
lsof +L1
echo 1234 > /root/worker-pid.txt(with the PID from the lsof output).
What each part does
rm /srv/app/logs: delete the broken symlink itself (it points nowhere).lsof +L1: list open files that have fewer than 1 link, meaning they were deleted but a process still holds them open and uses disk space.- The PID column tells you which process; save that number to the file.
Summary
- An inode is the file: metadata plus the location of its data. A directory maps names to inode numbers.
ls -liandstatshow inode numbers and link counts. - Hard link (
ln target name): another name for the same inode. Data lives until the last name is removed and the file is closed. Not for folders, not across filesystems. - Symlink (
ln -s target name): a file containing a path, typel. Works for folders and across disks; breaks if the target goes away.readlink -ffollows it. - Switch releases with
ln -sfn releases/NEW current. Find broken links withfind -xtype l.rmon a link removes only the link. df -ishows inode usage.lsof +L1finds deleted-but-open files; truncate logs instead of deleting them.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.