Learn / Linux / Getting Started
Lesson 9 of 74

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.

20 min read Report a problem Hands-on lab included 6-question quiz

A file's name is not the file

When you create /home/aditya/notes.txt, Linux stores two separate things:

  1. 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.
  2. A directory entry: inside the folder /home/aditya, a line that says "the name notes.txt means 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

Directory /srv/dataa table of names → inode numbers
report-q4.csv→ inode 1474720
latest-report.csv→ inode 1474720
report-q3.csv→ inode 1474721
Inode 1474720owner root · mode 644 · 36 bytes · modified 15:23 · links: 2 · data at block 88213
Inode 1474721owner root · mode 644 · 36 bytes · links: 1 · data at block 88214
Data blocksthe actual bytes: 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

bash
ls -li /srv/data

What -i does: adds the inode number as the first column.

text
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.csv and report-q4.csv share 1474720.
  • Column 3 (the one you skipped in earlier lessons): the link count, how many names point to the inode. It's 2 for the shared one and 1 for report-q3.csv.

stat shows the same information, plus everything else in the inode:

bash
stat /srv/data/report-q4.csv
text
  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 +0530

Inode: 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.

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.

bash
echo "test" > /root/a.txt
ln /root/a.txt /root/b.txt
ls -li /root/a.txt /root/b.txt
PartMeaning
lnmake a link. Without -s, it's a hard link.
/root/a.txtthe existing file (the target)
/root/b.txtthe new name
text
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

Same inode, link count 2. Edit one and the other "changes" too, because there's only one file.

Now delete one name:

bash
rm /root/a.txt
ls -li /root/b.txt
cat /root/b.txt
text
1474747 -rw-r--r-- 1 root root 5 Nov 24 15:23 /root/b.txt
test

The 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:

text
$ ln /srv/data /root/datalink
ln: /srv/data: hard link not allowed for directory
  1. No hard links to directories (it would create loops in the tree).
  2. 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.

Try it · Task 1 of 3

Inodes and hard links

Find the two report files that are really the same file.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -li /srv/data
PartMeaning
-llong format
-iadd the inode number as the first column
text
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 2 for both: that inode has two names.
  • report-q3.csv has its own inode and a link count of 1: a normal, separate file.

2. Confirm with stat

bash
stat /srv/data/report-q4.csv
text
  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

bash
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
CommandWhat it does
ln /root/a.txt /root/b.txtadds a second name, b.txt, for the same inode. No -s: a hard link.
rm /root/a.txtremoves one name. The data stays because another name still points to it.
text
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
test

Same 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).

2 · Your task

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:

text
printf 'one.csv\ntwo.csv\n' > /root/same-file.txt

What each part does

  • ls -li showed 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.
First start the lab machine in the terminal panel on the right.

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

Hard linkln data.csv backup.csv
data.csv
backup.csv
the same inodedelete either name: data survives
Symbolic linkln -s /srv/data.csv shortcut.csv
shortcut.csvown inode, contains "/srv/data.csv"
points to
/srv/data.csv
delete /srv/data.csvthe shortcut breaks
bash
ln -s /etc/hosts /root/hosts-link
ls -li /etc/hosts /root/hosts-link
PartMeaning
ln -screate a symbolic link
/etc/hoststhe target: the path the link will contain
/root/hosts-linkthe link's name
text
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/hosts has 10 characters).
  • Its permissions always show rwxrwxrwx. They don't matter: access is decided by the target's permissions.
Hard linkSymbolic link
Commandln TARGET NAMEln -s TARGET NAME
What it isanother name for the same inodea file containing a path
Own inode?no, shares ityes
Target deletedstill worksbreaks ("dangling")
Directoriesnot allowedallowed
Across filesystemsnot allowedallowed
In ls -llooks like a normal file, link count 2+type l, shows -> target
Used forbackups, snapshotsshortcuts, releases, config switching (much more common)
CommandShows
ls -l LINKthe link and its target
readlink LINKthe stored path, exactly as written
readlink -f LINKthe 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:

text
cd /srv/app
ln -s releases/v1.4 current              # current -> releases/v1.4 (relative)
ln -s /srv/app/releases/v1.4 current-abs # absolute

Relative 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 are everywhere on servers once you notice them:

WhereWhat the link does
/srv/app/current -> releases/v1.5zero-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/shopenable an nginx site by linking it, disable by removing the link
/etc/systemd/system/multi-user.target.wants/nginx.servicehow systemctl enable works (you'll see it in the systemd lessons)
/usr/bin/python3 -> python3.12pick which version a generic name runs (update-alternatives manages these)
/etc/localtime -> /usr/share/zoneinfo/Asia/Kolkatathe server's time zone
/bin -> usr/bincompatibility after /usr merge

Switching a release safely

bash
ln -sfn releases/v1.5 /srv/app/current
OptionWhy it's needed
-ssymbolic link
-freplace the existing current link
-nif current is a link to a folder, replace the link itself instead of creating a new link inside that folder
text
lrwxrwxrwx 1 root root 13 Nov 24 15:23 /srv/app/current -> releases/v1.5

No 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.

When the target disappears, the link stays and points at nothing:

text
$ 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 directory

Find all of them with:

text
find /srv -xtype l

Delete 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.

Try it · Task 2 of 3

Switch a release with a symlink

Point the app's current link at release v1.5 and create a shortcut to its config.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -l /srv/app
cat /srv/app/current/VERSION
text
total 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 shared
text
version=1.4
  • Lines starting with l are 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

bash
readlink /srv/app/current
readlink -f /srv/app/current
CommandPrints
readlink LINKthe path stored in the link, exactly as written
readlink -f LINKthe full, final absolute path after following every link
text
releases/v1.4
/srv/app/releases/v1.4

The 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

bash
ln -sfn releases/v1.5 /srv/app/current
ls -l /srv/app/current
cat /srv/app/current/VERSION
PartMeaning
ln -screate a symbolic link
-fforce: replace the existing link
-ntreat 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.5the target: what the link should point to
/srv/app/currentthe link's name
text
lrwxrwxrwx 1 root root 13 Nov 24 15:23 /srv/app/current -> releases/v1.5
version=1.5

Released. 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.

2 · Your task
  1. Switch /srv/app/current so it points to releases/v1.5.
  2. Create a symlink /root/config-link.yaml pointing 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
text
ln -sfn releases/v1.5 /srv/app/current
ln -s /srv/app/shared/config.yaml /root/config-link.yaml

What each part does

  • ln -s TARGET LINK: create a symbolic link (a pointer to the target path).
  • -f replace the link if it already exists, -n treat an existing link to a folder as a file to replace (otherwise ln would create a link inside the old folder).
  • Pointing current at releases/v1.5 is how many deployments switch versions in one step.
First start the lab machine in the terminal panel on the right.

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.

bash
df -i /

What -i does: shows inode usage instead of byte usage.

text
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.

bash
du -sh /var/log/shopapp
lsof +L1
CommandWhat it does
du -sh /var/log/shopapptotal size of the named files in the folder
lsof +L1list open files whose link count is below 1: deleted but still open
text
4.0K	/var/log/shopapp
text
COMMAND   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)
ColumnMeaning
COMMAND, PIDthe process holding the file: shop-worker, PID 28
FDfile descriptor 3, open for writing
SIZE/OFF31,457,280 bytes: 30 MB still on disk
NLINK0: no names left
NAMEthe old path, marked (deleted)

Every process also lists its open files in /proc/PID/fd:

text
$ 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:

text
truncate -s 0 /var/log/app/debug.log      # or:  > /var/log/app/debug.log

This 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.

Try it · Task 3 of 3

Broken links and deleted files

Remove the broken link and find the process holding a deleted log file.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

Two problems that inodes explain, and that you will meet on real servers:

  1. A broken (dangling) symlink points to a path that no longer exists. It still shows up in ls, but opening it fails.
  2. "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. du can't see it (no name), but df still counts it.

1. Find broken symlinks

bash
ls -l /srv/app/logs
ls /srv/app/logs/
find /srv -xtype l
CommandWhat it does
ls -l /srv/app/logsshows the link and where it points
ls /srv/app/logs/tries to open it: fails if the target is missing
find /srv -xtype lfinds links whose target doesn't exist (-xtype l: "still a link after following it", which only happens when it's broken)
text
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/logs

The 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:

bash
du -sh /var/log/shopapp
lsof +L1
CommandWhat it does
du -sh /var/log/shopappadds up the sizes of the files that have names in this folder
lsof +L1list open files with a link count below 1: files that have been deleted but are still open
text
4.0K	/var/log/shopapp
text
COMMAND   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)
  • du says the folder holds almost nothing.
  • lsof finds the truth: process 28 (shop-worker) still has debug.log open for writing (3w = file descriptor 3, write). SIZE/OFF is 31,457,280 bytes (30 MB), NLINK is 0 (no names left) and the name ends in (deleted).

You can see the same thing in /proc, where every process lists its open files:

bash
ls -l /proc/$(pgrep -x shop-worker)/fd
text
l-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.

2 · Your task
  1. Remove the broken symlink /srv/app/logs.
  2. Find the PID of the process that is holding the deleted debug.log open, 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
text
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.
First start the lab machine in the terminal panel on the right.

Summary

  • An inode is the file: metadata plus the location of its data. A directory maps names to inode numbers. ls -li and stat show 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, type l. Works for folders and across disks; breaks if the target goes away. readlink -f follows it.
  • Switch releases with ln -sfn releases/NEW current. Find broken links with find -xtype l. rm on a link removes only the link.
  • df -i shows inode usage. lsof +L1 finds deleted-but-open files; truncate logs instead of deleting them.
Check your understanding

Quick quiz

Feedback