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

Kaushal KishoreKaushal Kishore
4 min read

๐Ÿ”— 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"

๐Ÿง  The Technical Deep Dive

  • 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

  • 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

  1. Backup Systems

     # Create space-efficient backups
     ln /var/log/important.log /backup/important.log.$(date +%Y%m%d)
    
  2. Package Management

     # Multiple package versions sharing the same binary
     ln /usr/bin/gcc-9 /usr/bin/gcc
    
  3. 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
    
  1. Configuration Management

     # nginx sites management
     ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
    
  2. Version Management

     # Easy version switching
     ln -s /usr/bin/python3.9 /usr/bin/python
     ln -s /usr/bin/node-16 /usr/bin/node
    
  3. Directory Shortcuts

     # Quick access to deep directory structures
     ln -s /var/www/html/project/src/components ~/work
    
  4. Cross-Filesystem Links

     # Link across different partitions
     ln -s /mnt/external-drive/data /home/user/data
    

โš ๏ธ Common Pitfalls and Pro Tips

Pitfalls to Avoid:

  1. Hard link limitations: Can't cross filesystems

  2. Permission issues: Need proper permissions for both source and target

  3. Broken soft links: Always verify target exists

  4. Circular references: Avoid linking directories to their subdirectories

Pro Tips:

  1. Use ln -v for verbose output to confirm link creation

  2. Use readlink -f to resolve the full path of symbolic links

  3. Always test links after creation

  4. Use absolute paths for symbolic links when possible

๐Ÿ”ง Troubleshooting Common Issues

Solution: Check file ownership and permissions, use sudo if necessary

Issue: "File exists" error

Solution: Remove the existing file first or use different names

Solution: Use absolute paths and verify target exists

๐ŸŽฏ Key Takeaways

  1. Hard Links = Multiple names for the same file data

  2. Soft Links = Shortcuts pointing to file paths

  3. Data Persistence: Hard links preserve data, soft links depend on targets

  4. Use Cases: Hard links for data safety, soft links for flexibility

  5. System Understanding: Essential for Linux administration and DevOps


๐Ÿ“ Interactive Challenge

Try this yourself:

  1. Create a file with some content

  2. Make both hard and soft links

  3. Modify the original file - observe what happens

  4. Delete the original - see the difference

  5. 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! ๐Ÿš€

0
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.