How do you create a working Hard Link in OSX?

Ok, this has me banging my head. In OS X, to create a hard link, in a terminal window, you're supposed to be able to just type this...

ln a.txt b.txt

...and this should create a hard link from a.txt to b.txt. Simple and straight forward.

When I execute that statement, sure enough, in Finder b.txt appears right alongside a.txt. However, even after editing a.txt using TextEdit, b.txt still has the original data! In other words, it looks like it performed a copy, not a link, hard or otherwise.

Makes no damn sense to me! So can someone tell me what I'm doing wrong here?


The reason for this behavior is rather straightforward, and it relates to how files are saved in most Mac OS X applications: Atomically.

What happens is that a copy of the file is written to a temporary staging area, and then moved to replace the original file.

This, quite naturally, breaks hard links.


If appears, see this entry from superuser, that this is a feature of TextEdit. So, the normal method for creating hard links is correct, but TextEdit behaves in such a way as to break this. (The link says that it creates a new file and then moves it over the old file after editing.)


Note that emacs also exhibits this (startling!) behavior by default (in all unixes), thought the cause is slightly different (it is the "safe" thing to do in very low disk space situations). However, it can be configured in several ways.

See the documentation for make-backup-files (use C-h v make-backup-files) and for

  • backup-by-coping
  • backup-by-coping-when-linked
  • backup-by-coping-when-mismatch
  • backup-by-coping-when-privileged-mismatch