Hard links and Symbolic links — A comparison

Hard links and Symbolic links — A comparison

Image for postVisualized path difference between hard link and symbolic link references

Hard links and symbolic links are two different methods to refer to a file in the hard drive. These methods are part of the filesystem that organizes what file is what and where. A hard link is essentially a synced carbon copy of a file that refers directly to the inode of a file. Symbolic links on the other hand refer directly to the file which refers to the inode, a shortcut. In order to understand how symbolic and hard links work, we will need to go over what are inodes.

What is an inode?

The inode is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive. They are essentially the numerical equivalent of a full address. With an inode, the OS can retrieve information about the file such as permission privileges and the physical location of the data on the hard drive to access the file. Should a file be moved from one folder to another, the file will be moved to a different location on the hard drive and its inode value will change with it automatically. This will be important for hard links. Speaking of hard links?.

What is a hard link?

A hard link is a direct reference to a file via its inode. You can also only hardlink files and not directories. By using a hardlink, you can change the original file?s contents or location and the hardlink will still point to the original file because its inode is still pointing to that file. There is no referencing to the original file. In addition, hardlinks can only refer to files within the same volume otherwise symbolic links will be needed. To make a hard link of a file, you will require the ln command and refer to the source file before naming what the hard link will be named. Here is an example of how a hard link named test 2 will be made.

Image for postFirst made the test file before making hard link test2

The file test should be completely empty and I will add ?Hello? to it via the hard link.

Image for postTyping in Hello into the file test via test2Image for postOpening test 2 with Hello in itImage for postThe change from the hardlink is reflected in the original file

As seen in the photos above, I have changed the original file via the hard link by adding ?Hello?. By opening the original file, the word ?Hello? is already there. We can further make sure the files are referring to the same inode by using the ls -i command.

Image for posttest and test2 have the exact same inode.

Now what will happen if we copy over a similar file called test from a different folder into this folder? For this experiment we will change the folder name from ?test? to ?test folder?.

Image for post

Here we can see that the cp command does not change the inode value of the original value but mv does. We have copied over a file from the parent directory into ?testfolder? and the inode value has not changed. It is only when you move over a file and replace the file that the inode value changes.

What are symbolic links?

Symbolic links are essentially shortcuts that reference to a file instead of its inode value. This method can be applied to directories and can reference across different hard disks/volumes. Since the symbolic link is referring to the original file and not its inode value, then replacing the original file into a different folder will break the symbolic link, or create a dangling link.

Image for postMaking a symbolic link. Note the link has an arrow pointing to the original file in its filename

Since the symbolic link is a link that directs to the original file, changing the symboliclink should change the original file.

Image for postChanged the link hello_symImage for postThe change is reflected in the original fileImage for postA quick ls -i check shows they have different inodes

What will break a symbolic link is when the original file is moved to a different file or deleted.

Image for postMoving the original file to a different folder broke the linkImage for postOpening the link shows the link is broken

So symbolic links can be seen as a static link to the last known location of the original file. The link should work even if you replace the original file with a different file with the same name.

Image for postMade a new file hello with new contentsImage for postThe link is now working againImage for postContents of the link shows the contents of the new file

No Responses

Write a response