Difference Between Hard Link and Symbolic Link in Linux with Example

Difference Between Hard Link and Symbolic Link in Linux with Example

In Linux, the key difference between a hard link and a symbolic link (soft link) is that a hard link is a direct reference to the physical data (inode) on the disk, while a symbolic link is a shortcut or pointer to the file’s pathname. In other words, hard links share the same inode as the original file, whereas symbolic links simply point to the file name.

Understanding the difference between these two is crucial for Linux system administration, as it enables you to manage files more effectively, conserve disk space, and prevent accidental data loss. In this guide, we’ll dive deep into what hard links and symbolic links are, how they work, and provide step-by-step examples to help you master their usage.

What is an Inode in Linux?

Before we dive further, it’s important to understand the concept of an inode.

An inode is a data structure that stores metadata about a file, such as permissions, ownership, size, and disk block location.

The filename itself is not stored in the inode, but rather in the directory structure, which maps the filename to its corresponding inode number.

When you create a hard link, you are essentially creating another directory entry that points to the same inode. Symbolic links, however, create a new inode that stores the path to the target file.

Hard Link in Linux

A hard link is another name for an existing files. Both the original file and the hard link point to the same inode number.

Characteristics of Hard Links:

  1. They share the same inode numbers as the original file.
  2. Hard links cannot span across different files systems or partitions.
  3. If the original file is deleted, the data remains accessible through the hard links.
  4. They do not work for directories (to prevent filesystem loops).

Example of Hard Link:

# Create a file
echo "Hello Linux World" > file1.txt

# Create a hard link
ln file1.txt hardlink.txt

# Check inode numbers
ls -li

Output:

123456 -rw-r--r-- 2 user user 18 Sep 17 10:00 file1.txt
123456 -rw-r--r-- 2 user user 18 Sep 17 10:00 hardlink.txt

Here, both file1.txt and hardlink.txt share the same inode number (123456).
If you delete file1.txt, the contents will still be accessible from hardlink.txt.

rm file1.txt
cat hardlink.txt

Output:

Hello Linux World

Symbolic Link (Soft Link) in Linux

A symbolic link (or soft link) is a pointer to another file. Instead of pointing directly to the inode, it points to the file path of the target file.

Characteristics of Symbolic Links:

  1. They have their own inode number, different from the target file.
  2. Symbolic links can span across file systems and partitions.
  3. If the target file is deleted, the symbolic link becomes a “broken link” and no longer works.
  4. They can link to both files and directories.

Example of Symbolic Link:

# Create a file
echo "Hello Linux Symbolic Link" > file2.txt

# Create a symbolic link
ln -s file2.txt symlink.txt

# Check inode numbers
ls -li

Output:

123457 -rw-r--r-- 1 user user 26 Sep 17 10:10 file2.txt
123458 lrwxrwxrwx 1 user user 8 Sep 17 10:10 symlink.txt -> file2.txt

Notice that symlink.txt has a different inode number (123458) and points to file2.txt.

If you remove file2.txt, the symbolic link becomes broken:

rm file2.txt
cat symlink.txt

Output:

cat: symlink.txt: No such file or directory

Key Differences Between Hard Link and Symbolic Link

Here’s a comparison table for quick reference:

FeatureHard LinkSymbolic Link (Soft Link)
InodeSame inode as the original fileDifferent inode, points to target filename
Cross filesystemCannot span across filesystemsCan span across filesystems
DirectoriesCannot link to directoriesCan link to directories
File deletionData remains accessible via hard linkLink breaks if original file is deleted
SizeSame as original fileStores path length of target file
PerformanceFaster, direct inode referenceSlight overhead, follows path

Practical Use Cases

When to Use Hard Links:

  • When you want multiple filenames pointing to the same file data.
  • Useful for ensuring file availability even if the original is deleted.
  • Great for backup scripts and saving space.

When to Use Symbolic Links:

  • When you need to link files across different filesystems.
  • To create shortcuts for frequently accessed files or directories.
  • Commonly used in /etc, /usr/bin, and /lib for version management (e.g., linking python3 to python3.12).

Real-Life Examples in Linux

  1. Managing Versions
    ln -s /usr/bin/python3.12 /usr/bin/python

    Now running python will execute Python 3.12.

  2. Creating Shortcuts
    ln -s /var/www/html ~/mywebsite

    Access the website files directly from your home directory.

  3. Hard Link Backup
    ln important.doc backup.doc

    Even if important.doc is deleted, the content is preserved in backup.doc.

Advantages and Disadvantages

Hard Links:

Advantages:

  • Saves disk space.
  • Data persists even if the original file is deleted.

Disadvantages:

  • Cannot link across different partitions.
  • Not allowed for directories.

Symbolic Links:

Advantages:

  • Can cross filesystem boundaries.
  • Can link to directories.
  • Easy to identify (shows ->).

Disadvantages:

  • Breaks if the target is removed.
  • Adds slight overhead due to path resolution.

Conclusion

The main difference between a hard link and a symbolic link in Linux lies in how they reference files: hard links point directly to the inode, while symbolic links point to the filename. Hard links are best for redundancy and backup, while symbolic links are ideal for shortcuts and cross-filesystem linking.

By practicing with ln and ln -s, you’ll gain a strong understanding of when to use each, making you a more efficient Linux user or administrator.

Scroll to Top