Soft and Hard Links in Linux: The Ultimate Guide

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Understanding file links is fundamental for effective file management and data organization in Linux. Soft and hard links are two types of shortcuts to files that serve different purposes and exhibit distinct behaviors. This comprehensive guide will help you get acquainted with soft and hard links in Linux, their differences, and how you can utilize them in your daily tasks.

What is a Soft Link?

Soft links, also known as symbolic links or symlinks, are pointers to a file or directory. A symlink contains the path to its target. If the target file is moved, renamed, or deleted, the symlink will break and will not resolve properly. The command to create a symlink is ln -s.

# Create a file named 'original.txt'
echo "This is the original file." > original.txt

# Create a soft link called 'soft_link.txt' to 'original.txt'
ln -s original.txt soft_link.txt

# Display the contents via the soft link
cat soft_link.txt

Output: This is the original file.

What is a Hard Link?

Hard links, on the other hand, refer directly to the file’s inode on the disk, effectively creating another name for the same file content. Any changes made to the file content via any of the names will reflect across all of them. Importantly, hard links cannot cross file systems and cannot link to directories. To create a hard link, we use the ln command:

# Create a hard link called 'hard_link.txt' to 'original.txt'
ln original.txt hard_link.txt

Creating Hard Links and Soft Links

Understanding Inodes

Before diving deeper, it’s critical to understand the concept of inodes. Inodes are data structures used by the filesystem to store metadata about files. Each file has a unique inode number that represents it. Use the ls -i command to reveal the inode numbers.

ls -i original.txt hard_link.txt

Output: 123456 original.txt 123456 hard_link.txt

Visualizing Soft and Hard Link Differences

Let’s visualize how soft and hard links differ in terms of filesystem entries. A hard link creates a new directory entry for the same inode:

# The inode entries will be the same for the hard link and the original file
ls -i original.txt hard_link.txt

A soft link, alternatively, has its own inode and references the target path:

ls -i original.txt soft_link.txt

Output: 123456 original.txt 654321 soft_link.txt

Note that the soft link has a different inode number, which signifies that it’s indeed a separate file.

Behavior When Deleting or Moving Files

Understanding how soft and hard links respond when the target file is deleted or moved is crucial for their effective use.

Deleting the Original File

With a hard link, the content remains accessible, as the link points directly to the inode:

rm original.txt
cat hard_link.txt

Output: This is the original file.

However, with a soft link, accessing the data now gives an error:

cat soft_link.txt

Output: cat: soft_link.txt: No such file or directory

Moving the Original File

Moving the original file also affects links differently:

mv original.txt new_location/
cat soft_link.txt # Will break if the relative path has changed
cat hard_link.txt # Still works, since the inode number remains the same

Advanced Usage of Soft and Hard Links

Creating a Backup with Hard Links

One advanced usage of hard links is to create backups that don’t occupy additional disk space. The ‘cp -al’ command duplicates a directory tree using hard links:

cp -al /data /data_backup

This saves space since the content is not duplicated—the files in ‘/data_backup’ are hard links to the files in ‘/data’.

Creating Self-Healing Soft Links

It is possible to create soft links that automatically adjust to certain changes, such as a change in the working directory, by using relative paths:

cd /path/to/files
ln -s ../original.txt self_healing_link.txt

This link remains valid as long as the relative directory structure is maintained.

Scripting with Links

Shell scripts can harness the power of links for file management. For example, a script could selectively create symlinks to highlight files with certain attributes:

find /data -type f -size +10M -exec ln -s {} /large_files/ \\;

Conclusion

Soft and hard links are powerful tools for file management in Linux. Soft links offer flexibility and ease of use, while hard links provide a way to create multiple access points to the same data without using extra storage. As a Linux user, mastering both types of links allows you to organize files more effectively and harness the full potential of the filesystem.