Image for post
Image for post
Photo by Chris Ried on Unsplash

The difference between a hard link and a symbolic link

The command ln is for creating a hard link or symbolic link, more details about this command will be explained below. There are two kinds of link: a hard link, and a symbolic link. How are they different? Here are some key differences:

Image for post
Image for post
The very top row shows files that are visible to the user.

The above image shows how the files are linked to the index. As we can see, the hard link is connected to the same index, while the symbolic link is connected to a different index. This is why moving either the original file or the symbolic file would disrupt each other. Where as moving the hard link or original file would not disrupt each other as they both have the same index.

Hard link:

Referring from man ln: To create a hard link to a file from your current directory, the code will be ln originalFile linkName. The synopsis (command format) for creating both a hard link and symbolic link is as follows…

ln [OPTION]… TARGET… DIRECTORY

Our example file will be called test.js , and we want to create a hard link to it. So the code will be ln test.js hard_link_test.js .

Image for post
Image for post

Using ls -il we can see on the most left side the index number of each file, and in the example above: we can see that hard links have the same index number as the original file. This means that if either the hard link or original file are moved, neither of them will be affected by it because of the index being the same. As long as the index’s of both files are the same, editing either files will reflect off of each other.

Symbolic Link:

We will be using the same example file test.js , and we want to create a symbolic link to it. Referring from man ln , the option -s is what allows ln to create a symbolic link instead of a hard link. So the code will be ln -s test.js symbolic_link_test.js .

Image for post
Image for post

Using ls -il we can see that the index’s are different: which means that moving either files will affect one another because of the index’s being different. As long as the symbolic link is linked to the correct file name in the original destination, editing either files will reflect off of each other.

Written by

Software Engineer | Linux | Cats | https://github.com/kai-dg | https://haruspace.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store