Skip to content

Links

A link lets you access the same file from multiple locations. Linux has two types: hard links and symbolic links (symlinks).

What is an inode?

Every file has an inode (index node) - a unique number that stores metadata about the file.

ls -i filename.txt
# Output: 12345678 filename.txt
Command What it shows
ls -i Show inode numbers
stat filename Show detailed inode info

The filename is just a human-readable label. Linux tracks files by their inode number.


A hard link is another name for the same file (same inode).

ln source.txt link.txt   # source.txt must exist beforehand
Feature What it means
Same inode ls -i shows same number
Delete "source.txt" File still exists (link.txt still works)
Cannot cross filesystems Only works on same disk
Cannot link directories (except with special permission)

Visual

                  ┌────────────┐
            ┌────>│ source.txt │
            │     └────────────┘
  ┌─────────┴────┐
  │ DATA STORAGE │
  │ Inode: 12345 │
  └─────────┬────┘
            │     ┌──────────┐
            └────>│ link.txt │
                  └──────────┘

Essentially, two doors to the same room.


A symlink is a shortcut that points to another file (different inode).

ln -s source.txt symlink.txt

Feature What it means
Different inode ls -i shows different number
Delete source Symlink becomes broken (red/flashing)
Can cross filesystems Works across different disks
Can link directories ln -s /some/folder link

Visual

┌─────────────┐     ┌──────────────┐     ┌─────────────┐     ┌──────────────┐
│ symlink.txt │ ──> │  LINK DATA   │ ──> │ source.txt  │ ──> │ DATA STORAGE │
│ (Shortcut)  │     │ Inode: 67890 │     │  (Target)   │     │ Inode: 12345 │
└─────────────┘     └──────────────┘     └─────────────┘     └──────────────┘

Symlink = Shortcut

Like desktop shortcuts on Windows/macOS. It's a pointer to the real file.


If you want... Use
Backup that survives the original Hard link
Link to a directory Symlink
Link across drives Symlink
Shortcut for a long path Symlink

Every folder has at least 2 hard links:

  • The name itself - entry in the parent directory

  • . (dot) - points to itself

    Parent Directory
      │ (Link 1: The Name)
┌─────────────┐
│  YourFolder │ <────┐
└─────────────┘      │ (Link 2: The ".")
      │              │
      └──────────────┘

When a subfolder is created it now has 3 links because of ...

/home/your-user
┌─────────────┐
│   folder    │ (3 Links: .  ..  folder (the name))
└──────┬──────┘
       │  ↑
       │  │ (Link 3: The "..")
       ↓  │
┌─────────────┐
│ sub_folder  │
└─────────────┘
Command What it does
pwd -L or pwd Shows the symlink path (Logical - default)
pwd -P Shows the real path (Physical - resolves symlinks)
pwd -L   # /tmp/link (shows the shortcut)
pwd -P   # /home/username/real/folder (shows the real destination)
Command What it does
ln source link Create hard link
ln -s source link Create symbolic link
ls -i Show inode numbers
stat <file> Show detailed inode info
pwd -L Show logical path (follows symlinks - default)
pwd -P Show physical path (resolves symlinks)

Check Your Understanding

You delete source.txt. Which link still works?

Reveal Answer

The hard link. It shares the same inode, so the data remains. A symlink becomes broken.

You want to link a file from your SSD to a USB drive.

Can both hard links and symlinks do this?

Reveal Answer

Only symlinks. Hard links cannot cross filesystems.

You want to create a shortcut to /home/your-username/my-photos/animals/cute-cat.png.

What command do you use?

Reveal Answer

ln -s /home/your-username/my-photos/animals/cute-cat.png cat_photo

4. Finding the real path

You're inside a symlinked directory and pwd shows the shortcut path.

How do you see the real (physical) path?

Reveal Answer

pwd -P (Physical path, resolves symlinks)

A new empty folder has a link count of 2. What are they?

Reveal Answer
  1. The folder name (entry in parent directory)
  2. . (dot - points to itself)

You have symlink.txt pointing to source.txt. You move source.txt to a different folder.

Does symlink.txt still work?

Reveal Answer

No. The symlink becomes broken. Symlinks store the path, not the inode.