Linucate
~ Linucate_

104.6 Create and change hard and symbolic links

All Levels

Introduction

This lesson is about links.

The main question is:

How can one file name point to another file, and what is the difference between a hard link and a symbolic link?

What you should be able to do after this lesson:

  • Create hard links and symbolic links.
  • Tell them apart.
  • Explain when copying is different from linking.
  • Understand why broken symlinks are possible.

Big Idea: Names and Inodes Are Not the Same Thing

On Unix-like systems, the file name you see is not the file data itself.

At beginner level, keep this picture in your head:

directory entry -> inode -> file data

That one idea explains most of the lesson.

Hard link

A hard link adds another directory entry pointing to the same inode.

Symbolic link

A symbolic link is a special file that stores a path to another file or directory.

Simple memory trick:

  • hard link = another name for the same file data
  • symbolic link = a path-based pointer

Create a Hard Link

ln original.txt hardlink.txt

Create a Symbolic Link

ln -s original.txt symlink.txt

How to Tell Them Apart

Use:

ls -li

Sanitized example output:

total 8
2724 -rw-r--r-- 2 <user> <group>  5 Mar 23 22:53 hardlink.txt
2724 -rw-r--r-- 2 <user> <group>  5 Mar 23 22:53 original.txt
2725 lrwxrwxrwx 1 <user> <group> 34 Mar 23 22:53 symlink.txt -> /tmp/lpic-link-XXXXXX/original.txt

What this shows:

  • hardlink.txt and original.txt share the same inode number
  • the link count is 2, which means two directory entries point to that inode
  • symlink.txt has its own inode and points to a path with ->

What to notice:

  • hard links share the same inode
  • symbolic links have their own inode and show the target path

What Happens If the Original Name Disappears?

This is where beginners often understand the difference for the first time.

Hard link case

If you remove original.txt but hardlink.txt still exists:

  • the file data usually still exists
  • hardlink.txt still works
  • there was no single "real" name that mattered more than the others

Symbolic link case

If you remove the target of symlink.txt:

  • the symlink still exists
  • but its target path no longer works
  • now you have a broken symlink

That is why broken symlinks are common but broken hard links are not the normal picture in the same way.

Important Differences

Hard links

  • usually stay valid even if one name is removed
  • normally cannot span filesystems
  • usually are not used on directories in normal admin work
  • refer to the same inode and same file data

Symbolic links

  • can point across filesystems
  • can point to directories
  • can point to something that does not exist yet
  • break if the target path disappears or changes in the wrong way

Copying vs Linking

Copying

Creates another independent file with its own data and inode.

Hard linking

Creates another name for the same inode.

Symbolic linking

Creates a pointer that says "go look at this path".

That means:

  • editing a hard-linked file changes the shared file data
  • editing a copy changes only the copy
  • editing through a symlink usually changes the target file

Practical Admin Uses

Links are not just theory. They are used all the time in real systems.

Common examples:

  • make a convenient alternate path with a symlink
  • keep an old path working after software moves
  • point a command name to another binary
  • provide a stable path to a changing release directory

Practice Step by Step

1) Create and inspect a hard link
  1. Create: ln original.txt hardlink.txt
  2. Inspect: ls -li original.txt hardlink.txt
  3. Notice the same inode number.
  4. Notice the link count.
2) Create and inspect a symlink
  1. Create: ln -s original.txt symlink.txt
  2. Inspect: ls -l symlink.txt
  3. Notice the -> original.txt target.
3) Watch a symlink break on a test file
  1. Create a test file: touch demo.txt
  2. Create a symlink: ln -s demo.txt demo-link.txt
  3. Remove the target: rm demo.txt
  4. Inspect: ls -l demo-link.txt
  5. Notice that the symlink exists, but its target is gone.

Cheat Sheet

  • ln source hardlink = create hard link
  • ln -s source symlink = create symbolic link
  • hard link = same inode, same file data
  • symlink = path-based pointer
  • ls -li = compare inode numbers and link counts
  • hard links usually cannot cross filesystems
  • symlinks can point to directories and cross filesystems
  • broken symlink = symlink exists, target path does not work
🎯

Test Your Knowledge

Complete the quiz to assess your understanding of this course's concepts.