
Symptom
What is ln command? How to use ln command? What is different between Hard & Symbolic/Soft links?
Solution
From Wikipedia, ln is a standard Unix command used to create links (link) to files. Links allow more than one file name to refer to the same file, elsewhere. There are two types of links, both of which are created by ln:
• Symbolic links, which refer to a symbolic path indicating the abstract location of another file; and
• Hard links, which refer to the specific location of physical data.
These links behave differently when the source of the link (what is being linked to) is moved or removed. Symbolic links are not updated (they merely contain a string which is the pathname of its target); hard links always refer to the source, even if moved or removed.
Creating Hard Link
1. I’m in /home/itsiti directory. In the directory, i have 1 text file named hard.txt.
# ls hard.txt # more hard.txt THIS is hard.txt file
2. Now, i am going to hard link the hard.txt to another file named hard2.txt on the same directory.
ln hard.txt hard2.txt
# ls hard.txt hard2.txt # more hard2.txt THIS is hard.txt file
3. Next, i am going to add some words under in hard.txt file. Once you have saved, please view the hard2.txt. It will have the same/updated content as hard.txt. This is actually vice-versa, if you want change something on hard2.txt, it will change on hard.txt.
# vi hard.txt THIS is hard.txt file Hello!
# more hard2.txt THIS is hard.txt file Hello!
4. Now, if i deleted the hard.txt, does the hard2.txt will be deleted as well? No, the hard2.txt will still be there and as well of the content! You can try that.
Creating Symbolic/Soft Link
1. To create a soft, you must put argument -s so that it will read as symbolic/soft link instead of hard link. As an example, you want to soft link folder soft to folder soft2. In soft folder, you have 1 file & 1 folder (soft.txt & soft3/)
# ln -s /home/itsiti/soft/* /home/itsiti/soft2/
# cd soft2/ # ls -lrt soft3 -> /home/itsiti/soft/soft3 soft.txt -> /home/itsiti/soft/soft.txt
2. For symbolic/soft link, everytime you updated the original link, it will reflected on the target soft link. But, if the original/source link file is deleted, the target also will be removed.
Note
• By default, if no option/argument during the ln creation, it will automatically create a hard link.
• Symbolic link can be used for files, directories pointing. It also can works on different filesystem. But, it will deleted the target link if the source link is deleted.
• Hard link can still occupy the target link even the source link has been deleted.


