Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
3  
This question has been asked on Unix&Linux stackexchange: Why do hard links exists? –  Flimm Dec 6 '12 at 9:47

2 Answers 2

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.

share|improve this answer

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|improve this answer
    
"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. –  Paddy Landau Dec 6 '12 at 15:13
1  
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 ;) –  Rinzwind Dec 6 '12 at 17:57
2  
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. –  Paddy Landau Dec 6 '12 at 21:29
    
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. –  Ken Sharp May 25 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.