ln is the Linux command that allows you to create links. For understanding purposes, you can compare this to the windows equivalent of creating a shortcut.
There are two types of links that can be created with the ln command:
- Symbolic links
- Hard links
Creating Hard Links
Files on Linux and Linux like operating systems consist of two parts:
- The actual data of the file.
- The name of the file.
The data of the file is related to the ‘inode’ an inode is a reference point on the disk where the data is actually present whereas the name of the file portion is just a link to the inode plus the name of the file. We can access the same inode from more than one file name and processing of doing so is known as hard linking the files.
For creating hard links we don’t need to know the inode numbers. Suppose we have a file /tmp/age.txt and we to create a hard link to it with name agelink.txt, the command will be simply:
# ln /tmp/age.txt /tmp/agelink.txt
Now, agelink.txt is a hard link, as the hard link references the actual data on the disk so even removing the age.txt will not have any effect on agelink.txt, Linux only removes the actual data of the file when all files names pointing to the data are removed. So to free up the space used by age.txt we will have to remove both age.txt and agelink.txt.
Symbolic Links or Symlinks
Symbolic links are exactly same as Windows Shortcuts and also server the same purpose, hard links can be only created for files whereas symbolic links can be created for both files and directories. To create a symbolic link to file /tmp/age.txt, use the ln command with -s switch:
# ln -s source destination
In our example:
# ln -s /tmp/age.txt agelink
Now, agelink is a symbolic link to the /tmp/age.txt file. unlike the hard link, deleting the original /tmp/age.txt file will render the symbolic link unusable. In a practical world, most of the time we deal with symbolic links, instead of hard links.