What is the Difference Between Hard and Soft Links

The main difference between hard and soft links is that each hard-linked file is assigned the same Inode value as the original so that they refer to the same physical location while each soft linked file contains a separate Inode value that points to the original file.

In UNIX, a link is a pointer to a file. Therefore, a link points to a file or a directory. Creating links is a kind of shortcut to access a file. Links allow more than one file name to refer to the same file elsewhere. Generally, there are two types of links as hard links and soft links.

Key Areas Covered

1. What are Hard Links
      -Definition, Functionality
2. What are Soft Links
     -Definition, Functionality
3. Difference Between Hard and Soft Links
     -Comparison of key differences

Key Terms

Hard Links, Inode, Pointer, Soft Links, UNIX

Difference Between Hard Links and Soft Links - Comparison Summary

Each hard-linked file is assigned the same Inode value as the original. Inode refers to a file structure on a file system. Therefore, they refer to the same physical file location. Generally, hard links are more flexible. The links remain the same even if the original or linked files are moved to some other location of the file system. However, it is not possible to move to different file systems.

Difference Between Hard and Soft Links

These links have the actual file contents. If the user removes the links, it does not minimize the link count. It will only affect other links.  Even though the original file is removed, the link will still display the content of the file. Furthermore, it is not possible to create a hard link for a directory to avoid recursive loops.

Each soft linked file contains a separate Inode value that points to the original file. It is possible to link the soft links across different file systems. If the user deletes or moves the original file, then the soft linked file will not work properly. The link always points to the original file. In other words, a soft link has the path for the original file but not the contents.

If the user removes the soft link, it does not affect anything. However, removing the original file will make the link become a “dangling” link. It will point to a nonexistent file. Moreover, a soft link can link to a directory. Additionally, if the user wants to link files across the file systems, then he can use only these soft links.

Definition

A hard link is a directory entry that associates a name with a file on a file system. In contrast, a soft link is a  term for any file that contains a reference to another file or directory in the form of an absolute or relative path that affects the pathname resolution.

Content

Hard links have actual file contents whereas soft links contain the path for the original file and not the contents.

Command

Command to create hard links is $ ln [original filename] [link name] while command to create sort links is $ ln  -s [original filename] [link name].  

Effect on removal 

Removing the hard link will show the content of the original file while in soft links, removing the original file will make the link to become a dangling link which points to a nonexistent file.

Linking

It is not possible to link the files across different file systems using hard links. On the other hand, it is possible to link the files across the file systems using soft links.  

Conclusion

In brief, the main difference between hard and soft links is that each hard-linked file is assigned the same Inode value as the original so they refer to the same physical location while each soft linked file contains a separate Inode value that points to the original file.

References:

1.“Hard Link.” Wikipedia, Wikimedia Foundation, 20 May 2019, Available here.
2.“Symbolic Link.” Wikipedia, Wikimedia Foundation, 29 July 2019, Available here.

Image Courtesy:

1.” Simplified illustration of hard links” By Mtodorov 69 – On Athlon Phenom x 4; Ubuntu 32-bit; LibreOffice Draw; export to PNG.Previously published: This work had not yet been published (CC BY-SA 4.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply