Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

RHEL 6

My understanding is that hard links include a copy of the original file and that I could delete a hard-linked file in one directory, and it would still exist in the other directory.

If that's the case, why would I want to use hard links at all? Why not just have two separate files?

share|improve this question

4 Answers 4

If you copy a file it will duplicate the content. So if you modify the content of a single file, that has no effect on the other one.

If you do a hardlink that will create a file pointing on the same content. So if you change the content of either of the files the change will be be seen on both.

share|improve this answer

A hard link is basicly a second filename for the same file. So if you hardlink a file, it will only be once on the filesystem, and therefore only take up space once. So you want to use this if you wish to save diskspace

share|improve this answer
3  
Thanks for the prompt response. Then why would I want to use a hard link instead of a soft link? –  Mike B Feb 16 '13 at 18:11
5  
A softlink is basicly a pointer to another file. So if you delete the original file, the softlink will still exist but will be broken. A hardlink are 2 names for the same file, so they can be individually deleted. –  Peter Feb 16 '13 at 18:13
13  
Peter's explanation is good, but he's left out "link count". In the file's inode (on disk metadata) there's a link count. A hard link increments the link count, a soft link doesn't. The kernel is allowed to delete a file's contents if the link count drops to zero. –  Bruce Ediger Feb 16 '13 at 18:15
4  
I think Alan's answer to this has an important point: changing a file changes all it's hardlinks, with a copy they would have different content. –  donothingsuccessfully Feb 16 '13 at 18:33

On unix file systems every filename is actually a hard link to the location of the data on the disc, called an inode. If you create a new hard link to an existing file, it will take no extra space on the disk as it is just another pointer to the same data. If you edit the data by one or other link (or edit the in ode directly) both files will be changed.

The system keeps a count of how many hard links each in ode has. When the link count is 0, the file can no longer be reached, and the data is marked as safe to be overwritten. So given a file with 2 hard links, if you delete either link, the data will not be deleted. Only if you delete both will the data be gone.

A soft link, on the other hand, points to another file by its file name. If you move or delete the original file, the link will be broken.

share|improve this answer

With reference to the part of the question that asked, "why would I want to use hard links at all?":

Hard-linked files (or for that matter soft (symbolic)-linked ones offer a useful way of having a single executable file that can be made to masquerade for different purposes.

That is, the name by which the code is invoked can be examined in order to determine what options are available for execution. This allows the development and packaging of one large piece of code with all the shared functionality needed for slightly different purposes. From the user's perspective, the specification of "what" to run (by name) limits the choices and presentation to a more manageable subset of options.

A classic example is LVM. At one time HP-UX used hard-linked files for the various executables (e.g. vgdisplay, vgcreate, vgextend, etc. Today, like Linux, these commands are actually symbolic (soft) links to the lvm executable.

share|improve this answer
    
This has nothing to do with the question. –  rjmunro Feb 16 '13 at 23:06
    
@rjmunro I beg to differ. The OP asked, "why would I want to use hard links at all?" –  JRFerguson Feb 17 '13 at 7:14
    
I don't understand the last example. Once, there were hard links to lots of executables, now there are soft links to one executable. How are those soft links distinguishable? –  Emanuel Berg Feb 18 '13 at 3:39
    
@EmanuelBerg My point was that regardless of whether hard or soft links are used, it's the name by which the executable is invoked that determines the options and arguments that are valid for execution. –  JRFerguson Feb 18 '13 at 3:47
    
Yes, but if you have a bunch of soft links to the same executable, won't they do exactly the same, or can you somehow tag the soft links with different command line options? –  Emanuel Berg Feb 18 '13 at 22:54

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.