Similar to the pointers in the programming languages, there are links in UNIX/LINUX which are just like pointers which point towards a directory or a file. To generate these links is like creating shortcuts for accessing a file. With links, multiple file names can be referred to that same file, somewhere else.
These links can be of two kinds:-
- Hard Links
- Symbolic or Soft Links
Both these links show a different kind of behaviour when what they are linked to (source) is removed or shifted. The hard links can point out to the source even if it is shifted or erased while the symbolic links just consist of a pathname of the target file- they are not updated.
For instance, there is a .txt file. After creating a soft or symbolic link to the file, we erase the file, that file cannot be accessed through the symbolic link while the link also starts drooping. On the other hand, if a hard link is generated of the file and the file is erased, it can still be accessed through the hard link.
Simply framed, the symbolic link acts like a shortcut for the file while the hard link tends to grow the count of references of a location.
Basically, hard link is an extended name for the existing files in Unix or Linux operating systems. So it is like as many hard links can be generated i.e. as many names can be generated for any file. Hard links can also be generated for other hard links. But, hard links cannot be made for directories, neither can they cross partitions or go across the file systems. What the hard links do is that they can easily let the files, scripts, programs to be accessed from the ready to run version of that file (original file) in some other directory. On typing the hard link’s name the script, file or the program can be accessed and run just like the original file. To generate a hard link, this command is to be used- ln file_name hardlink. Through hard link, the components of the file are saved.
Hard links contains same inodes number. Hard links contain all the contents of the actual file and even if the original file is erased, the components of the file still show.
Also known as the Soft link or Symlink, the symbolic links uses the path of the existing file to link it. Linux and Unix creates the symbolic link using the command ln. Talking about the Windows, mklink command is used. Soft link is in the format of ASCII and the extension that is used is .slk taken in use by Microsoft applications. Data of the files can be exchanged between these Microsoft applications. These links are allowed between varied file systems. If the symbolic link will be removed or erased, the file will not be removed, just the link will be removed to the node or the original file. Even if the file that has a symbolic link to it is removed, that link is considered to be a dead link. Creating symbolic link is simply means that the software can be used more easily.
Symbolic link is just a shortcut file. Usually a shortcut file points out to a specific data or object, the symbolic link makes that the existing file is actually present there. When the symbolic link is generated on MacOS in the exact location as that of the original file, the message of ‘file exists’ pops up. On the contrary, if the symbolic link is generated somewhere else and then shifted to the same location as that of the original file, it exists as a copy of the original file.
Comparison between Hard Link and Soft Link: