Everyone who was used to Windows can imagine what symlinks are and how they're used. On the other hand, the hard link concept is foreign to Windows (am I correct?).
I would like to know what are the typical uses to hard links in Linux. I've already seen different posts describing the difference in how they work. What I'm asking is what are some typical situations when a user would be better off using hard links instead of symlinks?
I use a hard link where I need a single file in two or more places, I predict that one day I'll want to delete one of the locations, and I may forget that I have a link to the file. This prevents me from ending up with a symbolic link to a file that no longer exists.
Clarification:
A file name is, in fact, a hard link to the file. Thus, every file has at least one hard link, being what we normally think of as "the" file name. When you delete a file, in fact you are removing its hard link (hence the name "remove", i.e.
rm
, rather than "delete"). When a file has its last hard link removed, the system also deletes the file.Hard links allow ...
a single executable to have more than one name.
Example:
ls -l /bin | grep -v ' 1 ' | sort
will list the ones in/bin
for you. Result ...Instead of 3 files bunzip2 bzcat and bzip2 use the same file and inside the file the distinction is made to what to do. Saves code and less code means less possible bugs and easier maintenance.
a single file to be accessed by several paths.
Take for example a package manager, that creates a /usr/share/doc/$packagename directory for each package that is installed and inside that directory a file called LICENSE with the license information of the package. Many packages on a typical Linux system are GPL licensed, so instead of having 200 copies of the GPL on the filesystem there could be only one copy and 199 links. ptman@Serverfault
The reason why hard links work here (and soft ones do not): removing just 1 of the hard links does not remove the file itself.