What are some typical uses of hard links?
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 ...-rwxr-xr-x 2 root root 63 2010-01-19 21:49 gunzip -rwxr-xr-x 2 root root 63 2010-01-19 21:49 uncompress -rwxr-xr-x 3 root root 26300 2011-12-12 22:40 bunzip2 -rwxr-xr-x 3 root root 26300 2011-12-12 22:40 bzcat -rwxr-xr-x 3 root root 26300 2011-12-12 22:40 bzip2
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.