A link on Linux system is a connection between a file name and the actual data on the disk where this file is stored. Links in Linux systems are divided into hard links and soft links (also called symbolic links).
Soft links are references that refer back to an original file via its destination path. Directories can also be linked.
If the original file is deleted or its location is changed, the soft link will become a broken link and will point to nothing, becoming useless. By deleting the link instead, the original file remains intact, as only the reference to it is removed.
Let us now see in practice how to create a soft link using the terminal.
The command syntax to use is this:
ln -s original_path softlink_path
Let’s say that in our location we have a file called
dog.html and we want to create a soft link to it called
woff.html. The syntax to use is as follows:
ln -s dog.html woff.html
If we use the
ls -l command immediately after creating the short link, we will have the result shown in the image below:
As you can see, the link called
woff.html was created and points directly to
dog.html displaying an arrow symbol.
It is also possible to create a soft link for a file located in another directory. For example: I am located in the path
/home/mint/Documents/Papers and we wanted to create a link for the file dog.html which has the path
/home/mint/dog.html instead , the syntax we will write is as follows:
ln -s /home/mint/dog.html bark.html – I do not specify any path because as mentioned before, I am already in
We can still write a syntax with identical result using both absolute paths:
ln -s /home/mint/dog.html /home/mint/Documents/Papers/bark.html
And this is the result we will get from the ls command:
As you can see, the link points to a specific path, having been created in another location.
Same syntaxes can be used to link directories, if i write this line:
ln -s /home/mint/Documents/Papers /home/mint/papers_slink – i create a soft link to
Papers folder on
The Hard Link is not a true link but a mirror copy of the source file. Be careful, however, Hard Links are different from files obtained by the
cp command, but we will discuss this later.
Syntax for creating a hard link is:
ln original_path hardlink_path
The main differences from Soft Link are:
- It can only be created for files, not for directories;
- If you delete, rename, move, the original file, the hard link will continue to exist because it is a mirror copy, so it has within it the contents of the original file;
- The hard link has the same inode number as the original file and will have within it the exact contents of the original file as well as the same permissions, modification date, owner, etc: it is as if they were the same file.
Let us again use the example of the file
dog.html , wanting to create a hard link called
woff.html. Syntax will be (using absolute paths):
ln /home/mint/dog.html /home/mint/woff.html
To understand what we have just done, we use the command
ls -li and see the result in the image below:
We can see that
woff.html are two different files, but they both share the number
327, which represents the inode. Each hard link shares the same inode as the original file. If we delete the original file, the hard link will continue to exist and function properly, being a mirror copy of the original file. We also see that the two files share another number, namely
2, this number represents how many files are associated with the same inode, in this case the number is
2 because the inode
327 is associated with the files
However, if we modify any of the files associated with the above inode (either the original one or the hard link), the content also changes for all other files associated with the same inode.
The inode is deleted only if all the files associated with it are deleted.
The file bird.html as you can see has a different inode (
181) than the other two and the number of files associated with that inode is
1, showing that there is only this one file associated with inode 181.
Soft Links and Hard Links can be deleted by using the