Symbolic and hard links - What’s the difference?
Linux is a fairly flexible operating system that can be reflected in the creation of the links. But what are links? It is simply a way to create a shortcut to an original directory, which can be used in different ways, for example if we want to access a path that is in a hierarchical order deep in the directories with a single command line can be accessed.
However, it should be noted that there are two types of links. On the one hand, hard links can only link files (but not directories), a file cannot be referenced on a different disk and will still be usable, even if the original file is deleted.
The linux command for creating both hard links and symbolic links is “ln”, however, as you can imagine the use is totally different.
According to the ln manual, which can be found by typing the command “man ln”, there are 4 ways to create links which are
ln [OPTION]… TARGET LINK_NAME (1st form)
ln [OPTION]… TARGET (2nd form)
ln [OPTION]… TARGET… DIRECTORY (3rd form)
ln [OPTION]… -t DIRECTORY TARGET… (4th form)
How do we create hard links?
Using the first form, we can create a hard link in the following way:
We use the “ls -la” command to display the files and directories in long format. We can see that we have a directory called “code” and a file called “hello.txt”:
Later, we observe through the command “cat” what text is inside the file:
Now, we use the command “ln” to create the hard link. Then we define the file and, followed by it, the path and the name that we want to save this link:
We can see that the “code” directory is the file we just created:
Finally we visualize the file “hello_world.txt” and we check that it is the same text of the original file previously defined (“hello.txt”):
On the other hand, symbolic links, as mentioned above, are only used to link directories that could be on a different disk or volume. Unlike hard links, if the original file of the symbolic link is deleted it will make a broken link.
How do we create symbolic links?
Like hard links, we also use the “ls” command by adding the “-s” flag which means symbolic link. After that, you define the path of the directory you want to make the link, and then you define the path of the directory where you want to store it, as shown below:
REFERENCES