Difference Between Soft Link and Hard Link in UNIX in OS

A link in UNIX based system is used to establish connection between a file and the actual data on the disk. It’s more like a pointer or a reference that points to another file or a directory, just like the concept of pointers in programming languages.

Let’s say a file has a very long name and for some reasons, it cannot be changed. In UNIX, we can refer to the file with a reference using a shorter name for ease of use. These references to the same file in UNIX are called links. Simply put, a link is a means to refer to the contents of a file.

There are two types of links used in UNIX: Soft Links and Hard Links. Both are used basically for the same purpose except the way they behave.

 

What is a Soft Link?

A Soft Link, or a symbolic pink, is a small pointer file that associates a filename with the pathname. It is nothing but is a shortcut to the original file, similar to the shortcut option in the Windows operating systems. It simply serves as a reference to another file or directory without the actual contents of the file making it easy for you to simply delete the soft links without even affecting the contents of the original file or directory.

In simple terms, a soft link is usually an alias for the original file which redirects to the target file or directory when accessed through the pathname specified in the soft link’s subject. Additionally, you can use soft links to link across the filesystem.

 

What is a Hard Link?

A Hard Link is simply a replica of the original file which serves as a reference or a pointer to the same file thus making it easy to access even of the original file is removed or moved from its original location. Unlike soft links, changes made to the hard linked files will have effect on other files and the hard link still works even if you delete the source file from the system.

Hard links share the same Inode value and unlike soft links, they point to the file location instead of the directory. It is usually used in the file systems that allow more than on hard link for the same file. Simply put, a hard link is just an additional name for an already existing file on UNIX based systems.

 

Difference between Soft Link and Hard Link in UNIX

Meaning

A soft link, also termed as a symbolic link, is a special pointer that establishes links between files. It’s very similar to the shortcut feature available in the Windows operating systems. A soft link simply is a shortcut of a file that serves as a reference to another file or directory. A hard link is very different than a soft link. It serves as a reference or pointer to the original file meaning it is the exact mirror copy of the original file it is pointing to. Simply put, a hard link is just an additional name for an already existing file.

Behavior

As a soft linked file contains a separate Inode value pointing to the original file, the link becomes inaccessible if the original file or directory is removed from the system. It is just a shortcut to a file so you can delete the soft links without actually affecting the original file or directory. Unlike soft links, hard links share the same Inode value as the original pointing to the same file location. Hard links are more flexible than soft links because you can access the file even if the original file is removed from the system.

Command

To establish links between files in UNIX based system, you need to use the “In” command. A soft link refers to a symbolic path which indicates the location of the original file but not the contents. A soft link is created using a “In -s” command.

In -s <source_file> <myfile>

Here, you’d replace the ‘source_file’ with the original file for which you want to create a soft link and ‘myfile’ with the name of the soft link or symbolic link and the command In -s creates the soft link to a file name, both in the current directory.

On the other hand, “In” command is used to create a hard link. The In command is a standard UNIX command used to create a hard link to point to multiple files with the same name.

In <source> <linkname>

Structure

Unlike soft links which can be used to link both files and directories, hard links can only link files but not directories. The difference gives soft links certain qualities that hard links do not have, such as the ability to link across the filesystem. Creation and deletion of symbolic links won’t affect the original file. Hard links, on the other hand, cannot be used to link files outside the filesystem. Hard links share the same Inode value with actual file contents so removing any link won’t affect other links and it will still show the contents of the file.

Soft Link vs. Hard Link: Comparison Chart

 

Summary of Soft Link vs. Hard Link

In a nutshell, both soft links and hard links allow more than one file name to refer to the same file somewhere else, but the difference lies in the way they behave when the source link is removed from the system. A hard link takes less space than a soft link but any change made to the hard link will eventually reflect in the original file, whereas any changes to the soft link won’t have any effect on the original file. A soft link is like a shortcut feature of the Windows operating system used as an alias to refer to the original file or directory, whereas a hard link is like a replica of the original file that links two files in the same filesystem.