๐ Demystifying Linux Links: Hard Links vs Soft Links - A Hands-On Journey


๐ Demystifying Linux Links: Hard Links vs Soft Links - A Hands-On Journey
๐ Introduction
Have you ever wondered why some files survive deletion while others become "broken links"? Today, I'm taking you through a fascinating Linux experiment that reveals the secret behind Hard Links and Soft Links - a concept that's crucial for every DevOps engineer, system administrator, and Linux enthusiast.
What we'll learn:
The fundamental difference between hard and soft links
Real-world applications and use cases
Step-by-step hands-on demonstration
Pro tips for using links effectively
๐งช The Experiment Setup
Let's dive into the practical demonstration that will make this concept crystal clear:
# Starting in /home directory
cd /home
# Create our test file
sudo touch file1.txt
echo "Hi ! I am learning and exploring with confused dreams." | sudo tee file1.txt
# Create both types of links
sudo ln file1.txt file2.txt # Hard link
sudo ln -s file1.txt file3.txt # Soft link (symbolic)
# Verify all files exist and have the same content
ls -la
cat file2.txt # Shows: "Hi ! I am learning..."
cat file3.txt # Shows: "Hi ! I am learning..."
๐ญ The Plot Twist - Delete the Original!
Here's where the magic happens:
# Delete the original file
sudo rm file1.txt
# Now let's see what happens...
cat file2.txt # โ
Still works: "Hi ! I am learning..."
cat file3.txt # โ Error: "No such file or directory"
๐ Architecture Diagram: How Links Actually Work
๐ง The Technical Deep Dive
Hard Links Explained:
Direct inode reference: Points straight to the data on disk
Same inode number: Multiple names for the same file
Reference counting: File persists until all hard links are deleted
Limitations: Cannot cross filesystems or link directories
Soft Links (Symbolic Links) Explained:
Path reference: Points to a filename/path, not data
Different inode: Has its own inode that stores the target path
Flexibility: Can cross filesystems and link directories
Dependency: Breaks if target file is moved or deleted
๐ ๏ธ Verification Commands
# Check inode numbers
ls -li file1.txt file2.txt file3.txt
# Output example:
# 12345 -rw-r--r-- 2 root root 45 file1.txt
# 12345 -rw-r--r-- 2 root root 45 file2.txt โ Same inode!
# 67890 lrwxrwxrwx 1 root root 9 file3.txt -> file1.txt
# Count hard links
ls -l file1.txt # The number after permissions shows hard link count
# Find all hard links to a file
find . -inum $(stat -c %i file1.txt)
# Check if symbolic link is broken
file file3.txt
๐ Real-World Applications
๐น Hard Links Use Cases:
Backup Systems
# Create space-efficient backups ln /var/log/important.log /backup/important.log.$(date +%Y%m%d)
Package Management
# Multiple package versions sharing the same binary ln /usr/bin/gcc-9 /usr/bin/gcc
Deduplication
# Same file in multiple locations without duplicating data ln /shared/library.so /app1/lib/library.so ln /shared/library.so /app2/lib/library.so
๐น Soft Links Use Cases:
Configuration Management
# nginx sites management ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
Version Management
# Easy version switching ln -s /usr/bin/python3.9 /usr/bin/python ln -s /usr/bin/node-16 /usr/bin/node
Directory Shortcuts
# Quick access to deep directory structures ln -s /var/www/html/project/src/components ~/work
Cross-Filesystem Links
# Link across different partitions ln -s /mnt/external-drive/data /home/user/data
โ ๏ธ Common Pitfalls and Pro Tips
Pitfalls to Avoid:
Hard link limitations: Can't cross filesystems
Permission issues: Need proper permissions for both source and target
Broken soft links: Always verify target exists
Circular references: Avoid linking directories to their subdirectories
Pro Tips:
Use
ln -v
for verbose output to confirm link creationUse
readlink -f
to resolve the full path of symbolic linksAlways test links after creation
Use absolute paths for symbolic links when possible
๐ง Troubleshooting Common Issues
Issue: "Operation not permitted" when creating hard links
Solution: Check file ownership and permissions, use sudo
if necessary
Issue: "File exists" error
Solution: Remove the existing file first or use different names
Issue: Broken symbolic links
Solution: Use absolute paths and verify target exists
๐ฏ Key Takeaways
Hard Links = Multiple names for the same file data
Soft Links = Shortcuts pointing to file paths
Data Persistence: Hard links preserve data, soft links depend on targets
Use Cases: Hard links for data safety, soft links for flexibility
System Understanding: Essential for Linux administration and DevOps
๐ Interactive Challenge
Try this yourself:
Create a file with some content
Make both hard and soft links
Modify the original file - observe what happens
Delete the original - see the difference
Share your results in the comments!
#Linux #DevOps #SystemAdministration #UnixLinks #FileSystem #Learning #TechTutorial #HandsOn #justvisualise #100dayschallange
References: https://www.geeksforgeeks.org/linux-unix/soft-hard-links-unixlinux/
Have questions about Linux file systems or want to share your own experiments? Drop them in the comments below! Let's learn together! ๐
Subscribe to my newsletter
Read articles from Kaushal Kishore directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kaushal Kishore
Kaushal Kishore
Currently working as a Network Operations Center (NOC) Engineer at Verizon Networks under HCLTech as a Graduate Engineer Trainee (GET), I specialize in monitoring and maintaining critical network infrastructures. While I ensure seamless network uptime and resolve incidents at the provider level, I am also deeply passionate about transitioning into the DevOps space. With hands-on exposure to CI/CD pipelines, Docker, GitHub Actions, Ansible, and other modern DevOps tools, I am consistently upskilling to bridge the gap between operations and development. My journey reflects a dynamic shift from traditional NOC responsibilities to automation-driven DevOps workflowsโcombining reliability, efficiency, and innovation.