How can I create hard links in OSX?

8,704

Solution 1

Perhaps the editor is doing some weird things, like copying the file to some temporary location, or whatever.

My suggestion would be on the contrary: Edit the file using always the same path, and use symbolic links (ln -s) everywhere.

And version control systems might be relevant too.

Solution 2

Hard links work perfectly well on OSX:

$ echo foo > a
$ ln a b
$ cat a
foo
$ cat b
foo
$ echo bar >> b
$ cat a
foo
bar
$ cat b
foo
bar
$

It is likely a problem (or feature!) of your editor. A lot of editors perform atomic writes by writing to a temporary file and then moving that file over the top of the file you are editing. This means you wipe out the hard link:

$ echo baz > c
$ mv c b
$ cat a
foo
bar

I don't think there's an easy solution.

Share:
8,704

Related videos on Youtube

Lea Verou
Author by

Lea Verou

Updated on September 18, 2022

Comments

  • Lea Verou
    Lea Verou over 1 year

    What I want is to have multiple copies of the same file, and whenever I edit one of them, they all get updated. I thought Hard Links would solve my problem. So, I used the command ln as described in Wikipedia. However, when I changed the original file, the linked one didn't change.

    Did I misunderstand what hard links are supposed to do?

    • If yes:
      • Then what's the difference between ln and cp?
      • How can I do what I want?
    • If no:
      • Why didn't it work?
      • How can I get it to work?
    • Donald Duck
      Donald Duck over 6 years
      I think the problem is that the editor deletes the file and creates a new one with the same name. I read that here.
  • Lea Verou
    Lea Verou over 12 years
    If I use symbolic links in a repo (that point to a file outside the repo) and then push the repo to github, will others that clone the repo be able to use the file? That's why I wanted to use hard links.
  • Admin
    Admin over 12 years
    I would believe that yes, but the easiest thing is to try. And you could have the symlink made by the building process (e.g. with a Makefile rule).
  • Lea Verou
    Lea Verou over 12 years
    I think I get it, thanks to Basile: The editor saves the file to a different place in the disk every time. If it's just that file pointing to that space in the disk, the first place will get deleted. However, because there's a 2nd hard link pointing there, the OS leaves it there. So the first link points to the first file still, but the 2nd file now occupies another place in the disk.
  • Lea Verou
    Lea Verou over 12 years
    Unfortunately, turns out you can't even link to .js files that are symlinks :(
  • Admin
    Admin over 12 years
    I'm not sure to understand what you mean. Perhaps you need to configure the web server serving those .js files to follow symlinks.