What is a hard link and a symbolic link? (and the differences between them)
Did you know that in Unix-based systems, we can create something called hard links and soft links?
I’m going to tell you about them right now!
After all, it’s important that we understand the concept inode.
An inode contains metadata of a file, which is certain information of it such as size, permissions, modification time, owner, etc.
We can see them like an arrow pointing to where the file data is located:
So, now we should know that each file has its own link to its inode.
Let’s hop into…
What is a ‘hard’ link?
A hard link is just another file that is pointing directly to the same inode.
It’s crucial to know that hard links are only for files.
We can create a hard link with the command ln
But first, you need to define what will be its source and its destination.
It’s important to know, that you need to define the full path of both, otherwise, it will not work.
Symbolic Links (also called ‘soft’ links)
You can make soft links to files and directories, and we can also see them as a shortcut for the original file.
If we delete the original file, the soft link will not work anymore.
We create a soft link with the command ln -s
This image helps us to understand the concepts in a better way:
Let’s break down their differences:
- ‘hard’ links point to a file inode.
- ‘soft’ links are a shortcut for a file.
- ‘hard’ links are only for files.
- ‘soft’ links can be used with files and directories.
- ‘hard’ links will keep working if we decide to delete the original file.
- ‘soft’ links will break when we delete the original file.
Amazing! You’ve learned today about how the hard links and soft links work, how to create them, and in which situation you can find them useful.