I know what hard links are, but why would I use them? What is the utility of a hard link?
The main advantage of hard links is that, compared to soft links, there is no size or speed penalty. Soft links are an extra layer of indirection on top of normal file access; the kernel has to dereference the link when you open the file, and this takes a small amount of time. The link also takes a small amount of space on the disk, to hold the text of the link. These penalties do not exist with hard links because they are built into the very structure of the filesystem.
The best way I know of to see this is:
What this says is that when we go into a subdirectory and look at a different filesystem entry called
Hard links are the tendons that tie the filesystem's directories together. Once upon a time, Unix didn't have hard links. They were added to turn Unix's original flat file system into a hierarchical filesystem.
(For more on this, see Why does '/' have an '..' entry?.)
It is also somewhat common on Unix systems for several different commands to be implemented by the same executable. It doesn't seem to be the case on Linux any more, but on systems I used in the past,
I should point out that on most filesystems, users aren't allowed to make hard links to directories. The
One usage of hardlinks which is extremely useful is in incremental backups combined with rsync. It saves lot of space and makes the restoration procedure really easy. I use that approach for backup in my servers.
Take some time to read this explanation.
If after reading that wikipedia page your question is "why would I ever use them" then you don't understand what hard links are.
A link is a directory entry that points to blocks on disk. In other words every file on your system has at least one link. When you
You personally may not ever use hard links, but they are all over your system. For example:
You can see that
There are any number of uses. I use them to create file-based locks. The link(2) system call is atomic, unlike most other system calls.
Another use is within rsnapshot, where backups are taken over time using hard-links to reduce the amount of disk space. If a file has not changed, then the file is hard-linked to the older instances of the file, files that have changed are copied anew.
I also use them to swap out config files on servers:
To add to the several good discussions already present...
so we get them for free.