Overwrite a hardlink file should not delete its link reference

Suppose I have a file sample.txt, which has two hard links sample2.txt and sample3.txt. When I overwrite the old sample.txt with a new sample.txt, the hard link attribute of sample.txt disappears. The new file has no connection with sample2.txt and sample3.txt, which is what I don't want to see. The Windows built-in Explorer does not have this problem.

Is there any setting to avoid this?

1 Like

There isn't a setting for that.

Overwrite is done by deleting the existing file, then writing the new file in its place. (Rather than truncating the existing file to zero bytes and then writing the new data into it, which might be what Explorer is doing.)

I'm actually surprised Explorer works that way, as it doesn't make much sense to me. Completely replacing a hardlink with something else should not, IMO, affect other hardlinks to the same (old) data.

(Of course, as always with filesystem links, there are always arguments for them behaving both ways. It just doesn't seem like what should happen normally, IMO.)

I would instead use symlinks that point to the main file, since you can then replace it and the symlinks will continue to point to it.