What are some typical uses of hard links?

5,482

Solution 1

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.

Solution 2

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.

Share:
5,482

Related videos on Youtube

thorstorm
Author by

thorstorm

Ubuntu user since 2007.

Updated on September 18, 2022

Comments

  • thorstorm
    thorstorm over 1 year

    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?

  • Paddy Landau
    Paddy Landau over 11 years
    "Example: a move (mv) is a copy (cp) + a remove (rm)." This is true only when the source and destination are on different mounts. Otherwise, a mv moves only the name, not the file.
  • Rinzwind
    Rinzwind over 11 years
    If you want to go technical: mv changes the inode not the name. But you are correct: Looks like that example is a bit old nowadays and I should have used gzip as an example ;)
  • Paddy Landau
    Paddy Landau over 11 years
    We're getting a bit off-topic, but I've just tested this. As long as the file remains on the same mount, mv changes the name only; the inode remains the same.
  • Ken Sharp
    Ken Sharp almost 10 years
    This doesn't really explain why you can't just use symlinks - which you can save for a few bytes of extra data. This is correctly addressed in this answer.