So, I have a file, for this instance we shall call it $HOME/Documents/hello.txt. I will write some text inside it:

Hello, welcome to my text file

And I will hard-link this file here: $HOME/Documents/Backup/hello.txt.

Okay, great, this is now hard-linked. If I write to the original file, the hard-link will be updated:

echo "Hello again" >> $HOME/Documents/hello.txt
cat $HOME/Documents/hello.txt
Hello, welcome to my text file
Hello again
cat $HOME/Documents/Backup/hello.txt
Hello, welcome to my text file
Hello again

Now, my problem is, whenever I open either file (either of the hard-linked counterparts) with lots of programs that create temporary files, it loses its link relationship, and neither file will update the other anymore.

So, what can I do in this situation?

Note: I can not use symlinks in this situation, because I am using my hard link for Github to backup some files, and Git doesn't follow symlinks.

  • hard link ≠ symbolic link, which is it? Show directory listing in question. – ctrl-alt-delor Apr 20 '19 at 16:31
  • It's a hard link. – TheDeepNate Apr 20 '19 at 16:33
  • 2
    Re-link it with ln -f. There are many nasty problems with updating files in place, so that behavior of the editors (create a temporary new file, and atomically rename it to old name) is the best compromise, as it's the only one which doesn't lead to catastrophic data loss and corruption. – mosvy Apr 20 '19 at 16:43
  • 2
    What editor are you using? – Kusalananda Apr 20 '19 at 20:53

As mosvy already said in this comment, most editors do the edits in a copy of the original file which they replace (delete) later. While this increases security, it breaks hard links.

However, some editors like for example GNU Emacs can be configured to perform file edits in place, which means that they directly alter the original file, like you did in the shell. For example this Question and the corresponding answer discuss exactly your problem with respect to Gnu Emacs. So your editor's configuration would be the first point to look at.

Since you need the hard link only (?) for Git—unfortunately you are not very detailed on your workflow—, it is likely that you can use Git hooks to reestablish a correct hard link immediately before committing what you subsequently like to push to GitHub: The pre-commit hook seems to be a promising candidate for that. See man page githooks(5) for details.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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