Here is the video version, if you prefer it:
We talked about permanent storage devices, partitions, file systems and inodes. Today we are going to put all that knowledge to use and talk about the difference between symbolic and hard links.
Before we go into this, let me say that this is important to understand. If you ever encounter broken links, you have to know what is going on conceptually. So pay attention.
As we already know, a file is represented as an inode. More accurately, an inode tells you where the file is in the data pool (among other things), but in essence a file is represented by an inode.
We also already know what links are – links are pointers to files. There are two kinds of links – symbolic (also called soft links) and hard links. Let’s explain the difference.
Let’s use an example to illustrate this. This example is modeled after (“What is the difference between a hard link and a symbolic link?,” n.d.)
I already have two files to play with. Here are their contents:
mislav@mislavovo-racunalo:~/Linux_folder$ cat ab.txt
AB
Mustard is how we transfer wealth
Ab
aB
ab
mislav@mislavovo-racunalo:~/Linux_folder$ cat a.txt
A
a
aa
AA
Aa
aA
Some line
Now let’s create some links (both hard and symbolic):
mislav@mislavovo-racunalo:~/Linux_folder$ ln ab.txt ab-hard
mislav@mislavovo-racunalo:~/Linux_folder$ ln -s a.txt a-symbolic
The first command creates a hard link and the second command (with the -s
option) creates a symbolic link.
We can now access the files with the links to the files:
mislav@mislavovo-racunalo:~/Linux_folder$ cat ab-hard
AB
Mustard is how we transfer wealth
Ab
aB
ab
mislav@mislavovo-racunalo:~/Linux_folder$ cat a-symbolic
A
a
aa
AA
Aa
aA
Some line
Let’s look at ls -l
output:
mislav@mislavovo-racunalo:~/Linux_folder$ ls -l
total 40
...
-rw-r--r-- 2 mislav mislav 46 Feb 9 16:59 ab-hard
-rw-r--r-- 2 mislav mislav 46 Feb 9 16:59 ab.txt
...
lrwxrwxrwx 1 mislav mislav 5 Feb 12 07:33 a-symbolic -> a.txt
-rw-r--r-- 1 mislav mislav 26 Jan 13 05:18 a.txt
...
Look at the second column. It now says that link count for ab.txt
is 2, while link count for a.txt
is 1. How can this be? Shouldn’t link count for both ab.txt
and a.txt
be 2?
No, because hard links point to the underlying data and symbolic links are like shortcuts (shortcuts you may have used in Windows). So hard links point to the underlying data (which is an inode), while symbolic links point to the file.
I think that the difference is best explained with this figure, modeled after (“What is the difference between a hard link and a symbolic link?,” n.d.).
Here’s what happens when we rename ab.txt
(the one with the hard link):
mislav@mislavovo-racunalo:~/Linux_folder$ mv ab.txt ab-new.txt
mislav@mislavovo-racunalo:~/Linux_folder$ cat ab-hard
AB
Mustard is how we transfer wealth
Ab
aB
ab
Since ab-hard
points to the underlying data, it still works, even though the original filename changed.
Here’s what happens if we rename a.txt
(the one with the symbolic link):
mislav@mislavovo-racunalo:~/Linux_folder$ mv a.txt a-new.txt
mislav@mislavovo-racunalo:~/Linux_folder$ cat a-symbolic
cat: a-symbolic: No such file or directory
Since symbolic links just point to the filename, our a-symbolic
link is not working anymore. We now call it a broken link.
That’s it for today. Hope you learned something useful! Maybe you don’t see it yet, but when you will be dealing with broken links, it will pay greatly that you understand what is going on under the hood.
References:
What is the difference between a hard link and a symbolic link? (n.d.). Retrieved February 12, 2020, from https://askubuntu.com/questions/108771/what-is-the-difference-between-a-hard-link-and-a-symbolic-link