Hard Links
command: ln file-name hard-link-name
Hard links are the original Unix way of creating links. By default, every file has a single hard link that gives the file its name.
When we create a hard link, we create an additional directory entry for a file. Hard links have two important limitations:- A hard link cannot reference a file outside its own file system. This means a link cannot reference a file that is not on the same disk partition as the link itself.
- A hard link may not reference a directory.
When thinking about hard links, it is helpful to imagine that files are made up of two parts: the data part containing the file's contents and the name part which holds the file's name. When we create hard links, we are actually creating additional name parts that all refer to the same data part. The system assigns a chain of disk blocks to what is called an inode(you can see inode by "ls -i" command), which is then associated with the name part. Each hard link therefore refers to a specific inode containing the file's contents.
Symbolic Links
command: ln -s file-or-dir-name symbolic-link-name
Symbolic links were created to overcome the limitations of hard links. Symbolic links work by creating a special type of file that contains a text pointer to the referenced file or directory. In this regard, they operate in much the same way as a Windows shortcut though of course, they predate the Windows feature by many years ;-)
A file pointed to by a symbolic link, and the symbolic link itself are largely indistinguishable from one another. For example, if you write something to the symbolic link, the referenced file is written to. However when you delete a symbolic link, only the link is deleted, not the file itself. If the file is deleted before the symbolic link, the link will continue to exist, but will point to nothing. In this case, the link is said to be broken. In many implementations, the ls command display the broken symbolic link in different color.
The ls command shows that it is a symbolic link by the leading “l” in the first field.
Using relative path names in symbolic links is more desirable because it allows a directory containing symbolic links to be renamed and/or moved without breaking the links.