Objective

Create hard links and symbolic links, understand the difference in how they reference data, and know when to use each.

Tools & Technologies

  • ln
  • ls -l
  • inode
  • readlink

Key Commands

ln source hardlink
ln -s target symlink
ls -li file
readlink -f symlink

Architecture Overview

graph LR subgraph Hard Link HL[hardlink] -->|points to| IN1[inode 42\ndata blocks] F1[original] -->|points to| IN1 end subgraph Symbolic Link SL[symlink] -->|points to path| F2[original] F2 -->|points to| IN2[inode 99\ndata blocks] end style IN1 fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0 style IN2 fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0 style SL fill:#181818,stroke:#ffd700,color:#ffd700

Step-by-Step Process

01
Understand Inodes

Every file has an inode — a data structure holding metadata. Hard links share an inode. Symlinks store a path string.

# View inode numbers
ls -li /etc/hosts
# See link count
stat /etc/hosts
02
Create a Hard Link

Hard links are additional directory entries pointing to the same inode. Deleting the original doesn't remove data while any hard link exists.

echo 'test data' > original.txt
ln original.txt hardlink.txt
ls -li original.txt hardlink.txt  # same inode!
rm original.txt
cat hardlink.txt  # still works
03
Create a Symbolic Link

Symlinks store the path to the target. They break if the target is moved or deleted.

ln -s /var/log/syslog ~/syslog_link
ls -l ~/syslog_link        # shows -> target
readlink -f ~/syslog_link  # resolve full path
ln -s /etc/nginx/nginx.conf nginx.conf
stateDiagram-v2 [*] --> Created: ln -s target link Created --> Valid: Target exists Created --> Broken: Target missing Valid --> Broken: Target deleted/moved Broken --> Valid: Target restored
04
Compare Behaviour

Test what happens when the original is deleted for each link type.

# Hard link survives original deletion
rm original.txt && cat hardlink.txt  # OK

# Symlink breaks
rm target.txt
ls -l symlink.txt  # shows in red (broken)

Challenges & Solutions

  • Hard links cannot cross filesystem boundaries
  • Symbolic links to relative paths break if the link is moved

Key Takeaways

  • Use symlinks for config file management (e.g. /etc/nginx/sites-enabled/)
  • Hard link count in ls -l tells you how many names point to the inode