Difference Between Soft Link and Hard Link In Unix In OS (With Table)

In systems based on Unix, links act as a connecting bridge between original data present in the disk and the files and directories. For example, the name of a file is long. Moreover, the name is unchangeable. In such cases, the importance of pointers arises. Links are pointers that identify or provide references to the file or the contents of the file. The two most common links used in Unix are hard and soft links.

The main difference between a soft link and a hard link is the inode of the soft link doesn’t resemble the target file in any way. Soft links are functional across multiple file systems. Whereas hard links are not functional across multiple file systems. Moreover, directories can be linked-to soft links, whereas hard links for directories don’t exist.

A soft link in Unix & OS is simply a file that comes out of another target file. Thus, the symbolic link actually shows either a relative or an absolute path to the target file. These links act as pointers only to file names and are in no way related to their data or contents. One can use soft links to link any kind of directory or file. Soft links to an original file can also be identified as its alias. 

Hard link in Unix & OS points to the target file and provides it with an additional name. The hard link has the same contents as that of the original file. Thus, the contents and data of the file remain viewable even after the deletion or removal of the original file. The performance and flexibility of hard links are more as compared to that of soft links. Moreover, these links don’t consume any kind of extra space in the operating system.

Parameters Of Comparison

Soft Link In Unix In OS

Hard Link In Unix In OS

Definition

Soft links in Unix & OS refer to the files which are interconnected by links having varying inode numbers.

Hard link in Unix & OS refers to the files which are interconnected by links having the same inode number.

Speed

Their functioning speed is slow.

Their functioning speed is fast.

Advantages

The advantages of soft links are:Every file, file symbol object can be associated with a soft link.You can easily have varying metadata for the soft link with soft links.

The advantages of hard links are:Deletion, renaming, or moving target files to new locations doesn’t make the hard links worthless.It becomes more efficient and functional while maintaining varying inodes.

Disadvantages

The disadvantages of soft links are:Deletion or moving of target files to new locations makes the soft links worthless.It isn’t functional in all software due to some security concerns.

The disadvantages of hard links are:Hard links are difficult to be handled and dealt with.

Command

The command used for creating soft link in Unix & OS is:$ ln -s [original filename] [link name]

The command used for creating a hard link in Unix & OS is:$ ln [original filename] [link name]

A soft link, most popularly known as symbolic link and symlink, refers to the file which resembles the file shortcut feature and paves a path to some other directory or file. Soft links crawled into operating systems in 1978. The operating system identifies the text string of the soft link, which directs towards a target file or directory. 

The existence of a soft link is independent of the existence of the target file. The target file continues to exist and open properly even after the deletion of the soft link. However, if the name or address of the target file is modified in any way, the soft link remains unaffected and continues to direct the path to a location that doesn’t exist. In such cases, the soft link is known as the dangling link. 

Moreover, a soft link provides a path that directs towards the original target file. It has no connection with the contents of the file. The soft link’s size is exactly the same as that of the name of the target file. For example, if the target file’s name is abc123, then its soft link’s size will be 6 bytes. If the target file’s name is zebra 647, then its soft link’s size will be 8 bytes. 

A hard link, also known as a physical link, refers to a directory entry, which serves as the mirror replica of the target file, thereby assigning it a name. Every file has a hard link for itself. In simple terms, a hard link can be understood as a link that connects a file with its name in the operating system. Although it resembles a soft link in its function as a pointer, it differs in its versatility. 

The size of the hard link is the same as that of its target file. Moreover, on deleting the target file, the hard link remains unaffected. Thus, unlike soft links, hard links don’t become valueless or dangling links when the name of the target file is modified in any way. However, in such cases, the hard link will eat up the space of the original file, and thus, in these cases, it seems to be demanding more space.

One can create a hard link with the help of the CreateHardLink function. A file can have numerous hard links associated with it. Suppose an individual makes changes to the file using a specific hard link, then the attribute information, including the directory entry size, gets updated only for the link the individual used for making the changes.

  1. With a soft link, you can access a target file that has multiple references. However, the soft link fails to identify and trace the target file with multiple names. However, hard links can easily identify and trace such target files with multiple names. 
  2. The soft link becomes valueless after the deletion of the original file. However, the deletion of the original file doesn’t affect the validity of the hard link. It still points to the same file in the recycle bin.
  3. The inode numbers of the soft link and the original file are different. But, in the case of hard links, the inode number of the link is the same as that of the original file.
  4. While a soft link provides a relative path to a file, the hard link doesn’t provide the relative path to the file. 
  5. While soft links have slow functioning speed in Unix & OS, hard links have incredibly fast functioning speed in Unix & OS.

Conclusion

The soft link feature of Unix is similar to the shortcut feature of Windows. The file permissions of the target file and the soft link are different. Whereas the file permissions of the target file and the hard link are the same. The special symbol “I” dominates the file permission field in the case of a soft link file. But, no such special symbols exist in the case of hard link files. One can locate soft file links using the command; find / -type l. On the other hand, the command used for the location of hard file links is; find / -same file [source_filename]. While both these links are different in many ways, their main function is to act as pointers to specific files.

References

  1. https://www.usenix.org/event/usenix05/tech/freenix/full_papers/hensbergen/hensbergen_html/
  2. https://cloudflare-ipfs.com/ipfs/QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco/wiki/System_7.html