ln command Symbolic links and hard links

ln is the linux command that allows you to create links,for understanding purposes you can consider this to the windows equivalents of creating a shortcut

there are two types of links that can be created with the ln command

1) Symbolic links
2) Hard links

Creating Hard Links

Files on Linux and Linux like operating systems consist of two parts
1) The actual data of the file
2) 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 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.wri

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
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 practical world, most of the time we deal with symbolic links, instead of hard links

Leave a Reply