ln is for creating a hard link or symbolic link, more details about this command will be explained below. There are two kinds of link: a hard link, and a symbolic link. How are they different? Here are some key differences:
- A hard link cannot link to directories, while a symbolic link can
- A hard link cannot link across a different disk, while a symbolic link can
- A hard link can be moved to a different directory, while a symbolic link cannot. Same concept applies when moving the original file too.
The above image shows how the files are linked to the index. As we can see, the hard link is connected to the same index, while the symbolic link is connected to a different index. This is why moving either the original file or the symbolic file would disrupt each other. Where as moving the hard link or original file would not disrupt each other as they both have the same index.
man ln: To create a hard link to a file from your current directory, the code will be
ln originalFile linkName. The synopsis (command format) for creating both a hard link and symbolic link is as follows…
ln [OPTION]… TARGET… DIRECTORY
Our example file will be called
test.js , and we want to create a hard link to it. So the code will be
ln test.js hard_link_test.js .
ls -il we can see on the most left side the index number of each file, and in the example above: we can see that hard links have the same index number as the original file. This means that if either the hard link or original file are moved, neither of them will be affected by it because of the index being the same. As long as the index’s of both files are the same, editing either files will reflect off of each other.
We will be using the same example file
test.js , and we want to create a symbolic link to it. Referring from
man ln , the option
-s is what allows
ln to create a symbolic link instead of a hard link. So the code will be
ln -s test.js symbolic_link_test.js .
ls -il we can see that the index’s are different: which means that moving either files will affect one another because of the index’s being different. As long as the symbolic link is linked to the correct file name in the original destination, editing either files will reflect off of each other.