1

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.

5
  • hard link ≠ symbolic link, which is it? Show directory listing in question. Commented Apr 20, 2019 at 16:31
  • It's a hard link. Commented Apr 20, 2019 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. Commented Apr 20, 2019 at 16:43
  • 2
    What editor are you using? Commented Apr 20, 2019 at 20:53
  • I had the same problem with Mate editor. I have to use VIM whenever I edit a file with hard links to propagate the changes. Commented Jul 13, 2020 at 13:29

3 Answers 3

1

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.

0

As already explained by Jürgen and mosvy, the problem with hard links is that most editors do the edits in a copy of the original file which they replace (delete) later, ending with two different copies instead of propagating the changes.

To prevent this, a simple solution would be to place a group of hardlinks in the same directory that acts as an "index" for the files linked in different paths, and then prevent writing new inodes to this directory.

This can be accomplished either setting the permissions to read-only with:

chmod a-w . 

or by setting the locked attribute (works only under HFS+/APFS filesystems):

SetFile -a L . 

This way you/the editor cannot write any other file to this "index directory", but you can still modify your existing hard links if the editor allows it (e.g. by using VIM which allows to properly edit hardlinks). To add another hard link, just reset the write mode with the opposite commands (chmod u+w . or SetFile -a l .).

0

if you use vim with the following settings, it won't break the hardlinks.

vim -c 'set backupcopy=yes' <filename> 

or inside of it as follows.

:set backupcopy=yes 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.