Chapter 3.7 - Symbolic and Hard Links
Learning Objectives
By the end of this chapter, you will be able to:
- Understand the difference between symbolic and hard links
- Create and manage symbolic links
- Create and manage hard links
- Choose the right type of link for the case
What is a Link?
A link is a reference to a file. Linux supports two types of links: symbolic (soft) and hard.
Symbolic Links (Soft Links)
Characteristics
A symbolic link is a pointer to another file or directory.
# Create a symbolic link
ln -s /path/to/target symbolic_link
ln -s target.txt link.txt
# See where the link points
ls -l link.txt
readlink link.txt
Characteristics:
- Points to a file name (not the inode)
- Can point to non-existent files
- Can point to directories
- Can cross file systems
- Identified by
linls -l
Example
# Create a file
echo "Content" > original.txt
# Create a symbolic link
ln -s original.txt link.txt
# Verify
ls -li
# 12345 -rw-r--r-- 1 user group 8 Jan 1 12:00 original.txt
# 12346 lrwxrwxrwx 1 user group 12 Jan 1 12:01 link.txt -> original.txt
# Read via the link
cat link.txt # Displays "Content"
# Delete the original
rm original.txt
# The link still exists but is broken
cat link.txt # Error: No such file or directory
ls -l link.txt # link.txt -> original.txt (red, broken)
Advantages
- Can point to directories
- Can cross file systems
- Easy to identify (arrow in ls -l)
- Can point to non-existent files (for future creation)
Disadvantages
- Broken links if target is deleted
- Slightly slower (dereferencing)
- Can create loops
Hard Links
Characteristics
A hard link is a direct reference to the same inode as another file.
# Create a hard link
ln target.txt link.txt
# Verify (same inode)
ls -li
# 12345 -rw-r--r-- 2 user group 8 Jan 1 12:00 target.txt
# 12345 -rw-r--r-- 2 user group 8 Jan 1 12:00 link.txt
# Same inode (12345), same content
Characteristics:
- References the same inode
- Same file (multiple names)
- Cannot point to directories
- Cannot cross file systems
- Link count in
ls -l(2 in the example)
Example
# Create a file
echo "Content" > original.txt
# Create a hard link
ln original.txt link.txt
# Verify
ls -li
# Same inode, same size, same content
# Modify via a link
echo "New content" > link.txt
# Verify the original
cat original.txt # Displays "New content"
# Delete the original
rm original.txt
# The link still works!
cat link.txt # Displays "New content"
Link Count
# See the link count
ls -l file.txt
# -rw-r--r-- 3 user group 1024 Jan 1 12:00 file.txt
# The "3" indicates 3 hard links to this file
# The file is only truly deleted when
# the link count reaches 0
Advantages
- No broken link (even if one name is deleted)
- Performance identical to the original
- No dereferencing
Disadvantages
- Cannot point to directories
- Cannot cross file systems
- Less flexible
Comparison
| Characteristic | Symbolic Link | Hard Link |
|---|---|---|
| Inode | Different | Same |
| Directories | Yes | No |
| File systems | Yes | No |
| Non-existent file | Yes | No |
| Target deletion | Broken link | Works |
| Performance | Slightly slower | Identical |
| Identification | l in ls -l | Same inode |
Use Cases
Symbolic Links
When to use:
- Point to directories
- Create shortcuts
- Point to files on other file systems
- Flexible configuration
Examples:
# Shortcut to directory
ln -s /var/www/html ~/www
# Current version
ln -s app-v2.0 app-current
# Configuration
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
Hard Links
When to use:
- Safety backup (multiple names)
- Space saving (same file)
- Critical performance
Examples:
# Multiple names for the same file
ln file.txt backup.txt
ln file.txt archive.txt
# All point to the same inode
Useful Commands
# See all links to an inode
find /home -inum 12345
# Find broken symbolic links
find /home -type l ! -exec test -e {} \; -print
# Count hard links
stat file.txt | grep Links
# See where a symbolic link points
readlink -f link.txt # Resolved absolute path
Summary
In this chapter, you learned:
Symbolic Links: Pointers to file names (ln -s)
Hard Links: References to the same inode (ln)
Differences: Symbolic more flexible, hard more robust
Use cases: Symbolic for directories/flexibility, hard for robustness
Management: ls -li to see, readlink for symbolic
Next Steps
Chapter 3.8: File System Types
Lab 3.3: Search and Links
Chapter created on: December 2024