How can I create hard links in OSX?

2

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?

Lea Verou

Posted 2011-10-29T11:00:00.720

Reputation: 143

I think the problem is that the editor deletes the file and creates a new one with the same name. I read that here.

– Donald Duck – 2017-09-04T14:59:31.640

Answers

0

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.

Basile Starynkevitch

Posted 2011-10-29T11:00:00.720

Reputation: 1 022

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. – Lea Verou – 2011-10-29T11:17:52.107

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). – None – 2011-10-29T11:22:24.700

Unfortunately, turns out you can't even link to .js files that are symlinks :( – Lea Verou – 2011-10-29T11:41:00.293

I'm not sure to understand what you mean. Perhaps you need to configure the web server serving those .js files to follow symlinks. – None – 2011-10-29T11:48:18.410

0

Following the "tutorial" found on http://en.wikipedia.org/wiki/Ln_%28Unix%29#Hard_link, I've done the exact same commands on Mac Os X and it definitely does not work as it should be.

I thought at first that Mac Os X had a different behavior, so I tried on Ubuntu but I got exactly the same result :(

So the answer I can give you is "you did understand well according the explanation from wikipedia". Or perhaps we're both confused ?

Sorry I could not help you more :(

MaximeBernard

Posted 2011-10-29T11:00:00.720

Reputation: 101

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 – 2011-10-29T11:37:54.023