What operations breaks the hardlinks?

7

2

I have a file "a.txt" and its hardlink "ha.txt". If I open either of those files in notepad and save any changes it would appear in both files as they are still linked. But if I overwrite (copy) a.txt with another file with same name the hardlink breaks. What other operations do this?

Also, can I use it to backup a folder? Since if I ever change (overwrite) "a.txt" the hardlink automatically breaks and I have 2 copies. But because it depends on what I use to overwrite it (notepad certainly won't work) it ultimately depends on what operations in windows "breaks" the hardlinks and make copies?

laggingreflex

Posted 2012-03-16T05:36:04.580

Reputation: 3 498

Answers

5

Anything that deletes and recreates the target file will break the link. Some applications will do this, some won't. I don't think there's any way to predict this in advance.

Hard links are not a sensible backup mechanism, since there is only ever one copy of the file. Backups should always be to a different physical disk, anyway, in case the entire disk fails.

Harry Johnston

Posted 2012-03-16T05:36:04.580

Reputation: 5 054

Also, although obvious, if you modify the content of one "file", the hardlinked "backup" will be modified. Usually, you don't want your backup to change after the backup, don't you ? – xryl669 – 2014-12-16T19:14:11.427

See also this answer for some examples (especially in the comments): http://superuser.com/a/306832/242652

– GlennFromIowa – 2015-08-24T12:31:40.707

2

Anything that rewrites the filename will break the link. Modifying the file contents will not, nor will changing the filename metadata.

Ignacio Vazquez-Abrams

Posted 2012-03-16T05:36:04.580

Reputation: 100 516

1Could you clarify "rewrites the filename"? Renaming a file does not affect the hardlink. – user1686 – 2012-03-17T23:28:45.260

1One has to separate the concepts of "file name" and "file contents" in order to understand how hard links work. People have been conditioned by FAT/VFAT for so long into thinking that they are one and the same. The "file name" (which in reality is just a hard link) points to the "file contents" stored in inodes on disk. Removing and recreating the hard link in effect "rewrites" it. – Ignacio Vazquez-Abrams – 2012-03-18T01:50:11.527