What is the difference between a hard link and a symbolic link?
I recently just started full stack engineering school at Holberton where we were tasked with building both hard and symbolic links in Linux and I want to share it with other people so that they can understand how these links work from first principles. In this case I am going to break down how this concept is built upon the C programming language.
What is the difference between a hard link and symbolic link?
When we create a new file in Linux there are two main pieces of data that get “attached” to it:
- file name — self explanatory
- inode number — this is a lot like a struct in C. It holds values such as int node_no, int file_size, int user_id, int device_id, char mode, etc.
The way the operating system differentiates files is actually really similar to how international governments look up travelers when they enter the country — an identifying passport number. In the case of an operating system it happens to be called the inode number which is stored in something called an inode table. An inode also has pointers to disk blocks of the file’s actual data which can be seen below:
Lets see what this looks like in real life…
Looking at the diagram we can see that there is a pointer from the file -> inode table and then from the inode table -> disk drive. What this means is that you can break the link to the inode table by deleting the file but you still have a link from the inode table to the disk drive so the file is not realllyyyy deleted.
This is kinda like in C where you have a linked list and did not do one crucial thing:
The problem is that the memory space allocated for the link is not fully deleted (read: freed) and this causes all sort of issues.
Symbolic links differ from hard links in that they don’t actually hold any of the data of the file. They do this because they reference a memory address and don’t actually point to the disk space itself.
If you want to know more about the differences between hard links and symbolic links I have created a table to show the tradeoffs (see below):
(+) Deleting a symbolic link does not change the file’s data.
(-) Symbolic links will create a dangling pointer once the file is deleted which can cause programs to crash as one of the many pitfalls.
(+) Symbolic links work on both files and directories whereas hard links only work on files.
(+) Hard links are faster because they use pointers which make the algorithmic runtime for CRUD operations faster.
(+) Hard links take up less memory space, thus a better space complexity.
Hard Links — 2, Symbolic Links — 1 … Hard Links Win!
Creating both in the shell..
To create a hard link in Linux you should use the ln command. The following command creates a hard link named
tp to the file
$ ls -l
$ ln file1.sh tp
$ ls -l
To create a symbolic link in Linux you should again use the ln command but this time use the following flag
-s symbolic. The following command creates a symbolic link named
file1.sh to the file
$ ln -s ~/bin/file2.sh file1.sh
$ ls -l file1.sh
I hope that this tutorial was both fun to read and inspiring to learn more about these different files. If you want to learn more check out this link