Ravindra BagaleCourses & study guides मराठी Track your progress

Hard Link vs Soft Link in Linux: ln, ln -s and Inodes Explained with Examples

A file name is not the file. It is only a label that points to the real data. Linux keeps the real file details in an inode. Once you see this, hard links, soft links, 'disk full with free space' and many deploy tricks become easy.

What you'll learn in this chapter

  • What an inode is and what it stores
  • How to see inode numbers and link counts
  • Hard links: what, how, limits
  • Soft (symbolic) links: what, how, broken links
  • Hard vs soft link comparison
  • Real uses: releases, configs, versions

1. An everyday example: a locker and its keys

Why do we need this?

Rekha keeps gold in a bank locker in Kolhapur. The locker has a number. She has one key, and her son has a second key. If Rekha loses her key, the gold is still there because the son's key works. Separately, Rekha writes 'Locker 214, SBI Rajarampuri' on a note in her diary. If the bank moves the locker, the note is useless.

What is it?

The locker = the inode with data. Each key = a hard link (a real name for the same inode). The diary note = a soft link: it only stores a path. Delete the path and the note is broken.

How to do it (step by step)

Name to dataSoft link: current -> app-v2Name app-v2 (directory entry)Inode 1048 (owner, size, perms)Data blocks on disk
A soft link points to a name. A name points to an inode. The inode points to the data.

2. What is an inode?

Why do we need this?

Linux needs to store facts about each file (owner, size, time, where the data is) somewhere other than the name, so one file can have many names.

What is it?

An inode is a small record with a number. It stores type, permissions, owner, group, size, times (atime, mtime, ctime), link count and pointers to data blocks. It does NOT store the name. Names live in folders (directory entries) that map name → inode number.

How to do it (step by step)

  1. Show inode numbers.
ls -i

Output:

1048 notes.txt  1051 report.pdf

What it means: notes.txt is inode 1048.

  1. See the full inode details.
stat notes.txt

Output:

  File: notes.txt
  Size: 52        Blocks: 8     IO Block: 4096 regular file
Device: 259,1   Inode: 1048   Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ rekha)   Gid: ( 1000/ rekha)
Modify: 2026-10-25 10:30:12

What it means: Inode 1048, 1 link, 52 bytes, mode 0644.

  1. Check inode usage on a disk.
df -i /

Output:

Filesystem      Inodes  IUsed   IFree IUse% Mounted on
/dev/nvme0n1p1 1310720 210345 1100375   17% /

What it means: 17% of inodes are used. When 100%, no new files can be made.

Why do we need this?

Sometimes you want the same file in two places without using double the space, and you want it to survive if one name is deleted.

What is it?

ln TARGET NAME creates a hard link. Both names point to the same inode. The link count goes up. Data is freed only when the count becomes 0 and no process has it open. Limits: no hard links to folders, and not across file systems (different disks).

How to do it (step by step)

  1. Create a hard link.
ln notes.txt notes-copy.txt && ls -li notes*

Output:

1048 -rw-r--r-- 2 rekha rekha 52 Oct 25 10:30 notes-copy.txt
1048 -rw-r--r-- 2 rekha rekha 52 Oct 25 10:30 notes.txt

What it means: Same inode 1048. Link count is now 2.

  1. Edit through one name, read through the other.
echo 'new line' >> notes-copy.txt && tail -1 notes.txt

Output:

new line

What it means: They are the same file, not a copy.

  1. Delete the original name.
rm notes.txt && cat notes-copy.txt | tail -1 && stat -c '%h links' notes-copy.txt

Output:

new line
1 links

What it means: Data is still there. Count went back to 1.

  1. Try a hard link to a folder.
ln /etc etc-link

Output:

ln: /etc: hard link not allowed for directory

What it means: Folders cannot be hard linked (it could make loops).

  1. Try across disks.
ln /data/big.iso ~/big.iso

Output:

ln: failed to create hard link '/home/rekha/big.iso' => '/data/big.iso': Invalid cross-device link

What it means: Inode numbers are only unique inside one file system.

Why do we need this?

You want a short, stable name that points to a changing target, a folder, or something on another disk.

What is it?

ln -s TARGET NAME creates a symlink. It is a tiny file with its own inode that stores the target path. It can point to folders and across disks. If the target is moved or deleted, the link breaks (dangling). ls -l shows it with l and an arrow.

How to do it (step by step)

  1. Create a symlink to a folder.
ln -s /var/log/nginx ~/nginx-logs && ls -l ~/nginx-logs

Output:

lrwxrwxrwx 1 rekha rekha 14 Oct 25 10:40 /home/rekha/nginx-logs -> /var/log/nginx

What it means: l = link. The size 14 is the length of the path text.

  1. Different inode from the target.
ls -lid /var/log/nginx ~/nginx-logs

Output:

2231 drwxr-xr-x 2 root adm 4096 /var/log/nginx
1102 lrwxrwxrwx 1 rekha rekha 14 /home/rekha/nginx-logs -> /var/log/nginx

What it means: The link has its own inode 1102.

  1. See where a link points.
readlink -f ~/nginx-logs

Output:

/var/log/nginx

What it means: -f follows all links to the real path.

  1. Break a link and find it.
ln -s /tmp/gone.txt broken && cat broken; find . -xtype l

Output:

cat: broken: No such file or directory
./broken

What it means: find -xtype l lists broken links.

  1. Relative symlink (safe to move together).
cd /srv/app && ln -s releases/v2 current && ls -l current

Output:

lrwxrwxrwx 1 deploy deploy 11 current -> releases/v2

What it means: Relative links still work if /srv/app is moved or mounted elsewhere.

Why do we need this?

Interviews ask this every time, and picking the wrong one causes bugs.

What is it?

Compare them point by point.

PointHard linkSoft link
Commandln a bln -s a b
InodeSame as targetIts own
FoldersNot allowedAllowed
Across disksNoYes
Target deletedData staysLink breaks
ls -lLooks like a normal file, link count > 1l and ->
SizeSame dataLength of the path
PermissionsShared with targetAlways lrwxrwxrwx; target's perms apply

How to do it (step by step)

  1. See both side by side.
echo hi > a; ln a hard; ln -s a soft; ls -li a hard soft

Output:

2001 -rw-r--r-- 2 u u 3 a
2001 -rw-r--r-- 2 u u 3 hard
2002 lrwxrwxrwx 1 u u 1 soft -> a

What it means: hard shares inode 2001. soft has 2002.

  1. Delete the target.
rm a; cat hard; cat soft

Output:

hi
cat: soft: No such file or directory

What it means: Hard link kept the data. Soft link broke.

Why do we need this?

Links make deploys instant, keep many versions side by side, and keep configs tidy.

What is it?

Common patterns: a current symlink for releases (Capistrano style), sites-enabled links to sites-available in nginx, versioned libraries (libssl.so -> libssl.so.3), /usr/bin/python3 -> python3.11, alternatives on RHEL/Debian, and hard links in backup tools (rsnapshot, rsync --link-dest) to save space.

How to do it (step by step)

  1. Switch a release in one step.
ln -sfn releases/v3 current && readlink current

Output:

releases/v3

What it means: -f replaces, -n treats an existing link to a folder as a file. The switch is atomic enough for most apps.

  1. Enable an nginx site.
sudo ln -s /etc/nginx/sites-available/shop /etc/nginx/sites-enabled/ && sudo nginx -t

Output:

nginx: configuration file /etc/nginx/nginx.conf test is successful

What it means: Disable by deleting the link, not the file.

  1. See library version links.
ls -l /usr/lib/x86_64-linux-gnu/libssl.so*

Output:

libssl.so -> libssl.so.3
libssl.so.3

What it means: Programs ask for libssl.so; the link picks the version.

  1. Space-saving backups with hard links.
rsync -a --link-dest=/backup/day1 /data/ /backup/day2/ && du -sh /backup/*

Output:

5.0G /backup/day1
120M /backup/day2

What it means: Unchanged files are hard links, so day2 costs only 120 MB.

  1. Find all hard links of a file.
find / -xdev -samefile /data/report.csv 2>/dev/null

Output:

/data/report.csv
/data/archive/report-2026.csv

What it means: Both names share one inode.

7. Real examples from many industries

Why do we need this?

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

What is it?

Each row is a made-up team. It shows who uses this and how.

How to do it (step by step)

Team and cityHow they use itCommand
Karan, Rails team, PuneDeploys with a current symlink and instant rollbackln -sfn releases/20261025 current
Ingrid, hosting, OsloEnables nginx sites with symlinksln -s ../sites-available/blog sites-enabled/blog
Rahul, backup admin, AhmedabadDaily snapshots with rsync --link-destrsync -a --link-dest
Mei, Python team, ShanghaiPoints python3 to a new versionupdate-alternatives --config python3
Ahmed, mail server, CairoInodes ran out from millions of mail filesdf -i /var
Sara, data lake, StockholmLinks a big shared dataset into each projectln -s /mnt/datasets/sales ./sales

8. Common errors and how to fix them

Why do we need this?

Everyone hits these errors. Knowing the cause saves hours.

What is it?

Read the exact message first. Then match it to a row below.

How to do it (step by step)

Error you seeWhy it happensHow to fix
ln: failed to create symbolic link 'current': File existsA link or file with that name existsUse -sfn to replace it.
ln -sfn releases/v2 current
ln: /etc: hard link not allowed for directoryHard links to folders are not allowedUse a symlink.
ln -s /etc etc-link
Invalid cross-device linkHard link across file systemsUse a symlink or copy.
ln -s /data/big.iso ~/big.iso
No such file or directory (when opening a link)The link target is missing (dangling link)Check readlink and fix or remove.
readlink -f name
Too many levels of symbolic linksA link points to itself or a loopFix the target path.
ls -l name
nginx works for a while then 404 after deployRelative symlink resolved from the wrong folderRelative targets are relative to the link's folder, not yours. Use the right relative or absolute path.
ln -sfn /srv/app/releases/v2 /srv/app/current
No space left on device (df -h shows free space)Inodes are 100% usedFind folders with many files and clean.
sudo find /var -xdev -type f | cut -d/ -f2-3 | sort | uniq -c | sort -n | tail
rm current/ removed files inside the releaseA trailing / followed the link into the folderRemove a link without a trailing slash, or use unlink.
unlink current
Permission denied through a symlinkThe target's permissions apply, not the link'sCheck permissions on the target and its parent folders.
namei -l /srv/app/current/index.html

9. Troubleshooting step by step

Why do we need this?

Link problems come down to: where does it point, does that exist, and can I reach it?

What is it?

Go top to bottom. Stop at the first step that shows the problem.

How to do it (step by step)

  1. Is it a link?
ls -l name
  1. Where does it point?
readlink name
  1. Where does it end up?
readlink -f name
  1. Does the target exist?
test -e name && echo ok || echo broken
  1. Permissions along the path.
namei -l name
  1. Inodes left?
df -i .

10. Lab 1: Zero-downtime release switch

Why do we need this?

Haruto runs a ticketing site in Osaka. Deploy v2 next to v1 and switch with one symlink, then roll back.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Create releases.
mkdir -p ~/app/releases/{v1,v2} && echo 'Tickets v1' > ~/app/releases/v1/index.html && echo 'Tickets v2' > ~/app/releases/v2/index.html
  1. Point current to v1.
cd ~/app && ln -s releases/v1 current && cat current/index.html

Output:

Tickets v1
  1. Switch to v2.
ln -sfn releases/v2 current && cat current/index.html

Output:

Tickets v2
  1. Check the link.
ls -l current

Output:

lrwxrwxrwx 1 haruto haruto 11 current -> releases/v2
  1. Roll back.
ln -sfn releases/v1 current && cat current/index.html

Output:

Tickets v1
  1. Keep only the last 3 releases (preview).
ls -1t releases | tail -n +4

Why do we need this?

Show your team that a hard link is the same file, and see when data is really freed.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Make a 50 MB file.
dd if=/dev/zero of=big.bin bs=1M count=50 status=none && df -h . | tail -1

Output:

/dev/nvme0n1p1 20G 5.05G 15G 26% /
  1. Hard link it.
ln big.bin big2.bin && stat -c '%i %h %n' big*.bin

Output:

3300 2 big.bin
3300 2 big2.bin
  1. Delete one name; space is not freed.
rm big.bin && df -h . | tail -1

Output:

/dev/nvme0n1p1 20G 5.05G 15G 26% /
  1. Delete the last name; space is freed.
rm big2.bin && df -h . | tail -1

Output:

/dev/nvme0n1p1 20G 5.00G 15G 26% /

Ravindra Bagale's Tip

For deploys, always use ln -sfn with a relative path inside the app folder. -n stops ln from creating the new link inside the old target folder, the most common symlink bug.

12. Frequently asked questions

Q. Does a hard link use extra space?

No, only a tiny directory entry.

Q. Does deleting a symlink delete the target?

No. rm link removes only the link.

Q. What are . and .. in terms of links?

They are hard links made by the system. That is why a new folder has link count 2.

Q. How do I copy a symlink and not its target?

cp -P (or cp -a) copies the link itself. cp -L copies the target file.

Q. Can Windows read Linux symlinks?

Inside WSL yes. On a shared drive, often not.

13. Interview questions and answers

Beginner level

Q. What is an inode?

A record with file metadata and data pointers, but not the name.

Q. How do you create a soft link?

ln -s target linkname.

Q. How do you see inode numbers?

ls -i or stat.

Q. What happens to a soft link if the target is deleted?

It breaks.

Mid level level

Q. Hard vs soft link?

Hard: same inode, no folders, same file system, survives deletion. Soft: own inode, stores a path, can cross disks and point to folders, breaks if target goes.

Q. Why can't you hard link folders?

It could create loops and break tools that walk the tree.

Q. How do you find broken links?

find . -xtype l.

Q. What does ln -sfn do?

Creates or replaces a symlink and does not follow an existing link to a folder.

Senior level

Q. Disk is full but df -h shows space. Root cause and fix?

Inodes are exhausted. Check df -i, find the folder with many tiny files (sessions, cache, mail), clean and add rotation. Long-term: more inodes or xfs.

Q. You deleted a 20 GB log but space did not come back. Why?

A process still has it open. lsof +L1 shows it. Restart the process or truncate via /proc//fd.

Q. How do symlink deploys keep downtime near zero?

New code is fully prepared in a new folder. Switching the link is one rename-like step. Rollback is the same step back.

Q. Security risk with symlinks in /tmp?

Symlink attacks: an attacker links a predictable temp name to a sensitive file. Use mktemp and fs.protected_symlinks=1.

Try at home

  1. Make a file, a hard link and a soft link. Compare ls -li.
  2. Delete the original and see which still works.
  3. Run df -i on your machine.

Practice Tasks

About this scenario

Milan Furniture is a furniture business in Milan. Giulia Rossi looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 15

Easy Show inode numbers

Why: See which names share data.

What to do: Show the inode number of /etc/passwd.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Long list with inodes

Why: See inode and link count together.

What to do: List /var/www in long format with inode numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Full inode details

Why: Owner, size, times and links.

What to do: Show all inode details of invoice.pdf.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Create a hard link

Why: Two names, one file.

What to do: Create a hard link named data-link.csv for data.csv.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Create a soft link

Why: Short name for a long path.

What to do: Create a symlink named logs in the current folder that points to /var/log/nginx.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Where does a link point?

Why: Debug broken deploys.

What to do: Print the target stored in the link latest.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Which link survives?

Why: Know the difference.

What to do: You delete the original file. Which link still shows the data: hard or soft? Type one word.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Switch a release link

Why: Zero-downtime deploy.

What to do: Point the existing link live to releases/v7, replacing it safely.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Final real path

Why: Links can chain.

What to do: Print the fully resolved real path of the link current.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Find broken links

Why: Dangling links break apps.

What to do: Find all broken symlinks under /opt/app.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Find all symlinks

Why: Audit links in a project.

What to do: Find all symlinks (working or not) under /srv/milanfurniture.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Remove a link safely

Why: A trailing slash can delete the target contents.

What to do: Remove the symlink latest without touching its target.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Link count after ln

Why: The count tells how many names exist.

What to do: A file had link count 1. Giulia Rossi makes two hard links to it. What is the link count now?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Across disks

Why: Inodes are per file system.

What to do: You need a link from /home to a file on another disk /data. Which link type works: hard or soft? Type one word.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Find all hard links of a file

Why: Before deleting, know every name.

What to do: Find every name of /data/report.csv on the same file system, starting from /data.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. Name → inode → data.
  2. ln = hard link: same inode, same disk, no folders.
  3. ln -s = soft link: path pointer, can break.
  4. ln -sfn for release switches; df -i for inode issues.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

What is an inode. Explains links and disk full. Stores file data, not name. A name points to an inode. Show inode numbers.
Revision card: What is an inode · Download PNG · Open card page
Make links. One file, many names. ln = hard, ln -s = soft. Soft link stores a path. Point current to a release.
Revision card: Make links · Download PNG · Open card page
Hard link vs Soft link. Same inode, new name. / Small file with a path.
Comparison: Hard link vs Soft link · Download PNG · Open card page
rm vs unlink. Removes many names. / Removes one name.
Comparison: rm vs unlink · Download PNG · Open card page
Why no hard link to a folder?. It could make loops. Tools like find would hang. Use a soft link instead.
Interview card: Why no hard link to a folder? · Download PNG · Open card page
Broken soft link?. Target was moved or deleted. ls shows it in red. Find broken links.
Interview card: Broken soft link? · Download PNG · Open card page
Disk has space but no write?. Inodes are used up. Too many tiny files. Check inode use.
Interview card: Disk has space but no write? · Download PNG · Open card page
Deleted file still uses space?. A process keeps it open. Space frees when it closes. Find such files.
Interview card: Deleted file still uses space? · Download PNG · Open card page

More cards: Ravi Sir's Cards.