Thursday 3 July 2008

Symbolic Links vs. Hard Links

Many times, it is required that two or more applications have access to the same file somewhere on the file system. Microsoft users are familiar with those kinda files named icons that point to another directory or a file physically written on disk so when users run it, they actually run the file that icon points to. Links in Linux function that way, as a pointer to physical data. There are two kinds of links in Linux: hard links and symbolic links.
On most system all file names are hard links. The name of the file is just a name that refers to the actual data stored on the disk. It is possible to create multiple names (hard links) for the same data stored on the disk, so the data stored on the disk can be modified from different locations. Simply put, if one hard link is modified, all hard links (including the original file) is modified. Hard links on Linux are made with the ln command. If the file '.myconf' allready exists, to create a hard link named '.conf' we'll write:
ln /path/to/.myconf /path/to/.conf

If '.conf' is modified, '.myconf' is modified as well. All hard links pointing to same original file have same characteristics of that file such as, amount of disk space used, file permissions and so on. Drawback of hard link is that it is hard to make a difference between it and the real file because they show all real file characteristics. Also, all hard links need to be located on the same filesystem.
Symbolic links serves the same purpose as hard links, but they only contain a path to original file, not all characteristics of a file, so they are easy to differentiate. To create symbolic link type:
ln -s /path/to/original/file /path/to/symlink

Symbolic links are more used than hard links.

No comments: