IntroToLinux

4. Understanding Links

┌─────────────────────────────────────────────────────────────┐
│                       File System                           │
│                                                             │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐      │
│  │   Inode     │    │    Hard     │    │    Soft     │      │
│  │   (Data)    │◀───│    Link     │    │    Link     │      │
│  │             │    │             │    │      │      │      │
│  │ File Data   │    │  Same       │    │   Points    │      │
│  │ Metadata    │◀───│  Inode      │    │      │      │      │
│  │ Permissions │    │             │    │      ▼      │      │
│  └─────────────┘    └─────────────┘    └──────┼──────┘      │
│         ▲                                     │             │
│         │                                     │             │
│  ┌─────────────┐                              │             │
│  │  Original   │                              │             │
│  │   Filename  │                              │             │
│  └─────────────┘                              │             │
│                                               ▼             │
│                                    ┌─────────────┐          │
│                                    │  Target     │          │
│                                    │  Path       │          │
│                                    └─────────────┘          │
└─────────────────────────────────────────────────────────────┘

Hard Link Soft Link

Hard Links:                          Soft Links:
┌─────────────┐                     ┌─────────────┐
│ original.txt│─────┐               │ original.txt│
└─────────────┘     │               └─────────────┘
                    ▼                       ▲
             ┌─────────────┐                │
             │   Inode     │                │
             │   12345     │                │
             │             │                │
             │ File Data:  │                │
             │ "Hello..."  │                │
             └─────────────┘                │
                    ▲                       │
┌─────────────┐     │               ┌─────────────┐
│hardlink.txt │─────┘               │softlink.txt │
└─────────────┘                     │     │       │
                                    │  ─────────  │
Same inode = Same file              │ "original.  │
Delete original: hardlink works     │  txt"       │
                                    └─────────────┘
                                    
                                    Different inode
                                    Delete original: link breaks
# Create hard link
ln original.txt hardlink.txt

# Create soft link
ln -s original.txt softlink.txt
ln -s /path/to/directory linkname

# Create soft link with relative path
ln -s ../config/app.conf current_config

# Create multiple hard links
ln original.txt link1.txt link2.txt
Initial State:
┌──────────────┐    inode: 123456
│original.txt  │◀─── "Hello World"
└──────────────┘    

After creating links:
┌──────────────┐    ┌──────────────┐    inode: 123456
│original.txt  │◀───│hardlink.txt  │◀─── "Hello World"
└──────────────┘    └──────────────┘    

┌──────────────┐    inode: 789012
│softlink.txt  │ ─── "original.txt" ──┐
└──────────────┘                      │
                                      ▼
                           ┌──────────────┐
                           │original.txt  │
                           └──────────────┘

Link Count Check:
$ ls -l
-rw-r--r-- 2 user group 11 Oct 15 10:30 original.txt
-rw-r--r-- 2 user group 11 Oct 15 10:30 hardlink.txt
lrwxrwxrwx 1 user group 11 Oct 15 10:31 softlink.txt -> original.txt
           ^                                            ^
    Hard link count                              Link indicator
# Check link information
ls -li file*                     # Shows inode numbers
stat original.txt                # Detailed file information
file linkname                    # Shows link type

# Find all hard links to a file
find . -inum $(stat -c %i original.txt)

# Remove links
rm hardlink.txt                  # Removes link, file survives if other links exist
rm softlink.txt                  # Removes link only
unlink linkname                  # Alternative removal method
# Configuration management
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/

# Version management
ln -s /opt/java/jdk-11 /opt/java/current

# Backup strategy
ln important.txt backup_hardlink.txt    # Hard link backup

# Cross-directory access
ln -s ~/Documents/Projects ~/Desktop/Projects

# Temporary links
ln -s /var/log/application.log ~/current.log

Next: → Understanding Inodes
Previous: ← Archiving And Compression
Lesson Home: ↑ Lesson 6: Globbing & Archiving Course Home: ⌂ Introduction to Linux