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?

migrated from stackoverflow.com Oct 29 '11 at 18:08

This question came from our site for professional and enthusiast programmers.

  • 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 Sep 4 '17 at 14:59

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.

  • 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 Oct 29 '11 at 11:17
  • 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). – Basile Starynkevitch Oct 29 '11 at 11:22
  • Unfortunately, turns out you can't even link to .js files that are symlinks :( – Lea Verou Oct 29 '11 at 11:41
  • I'm not sure to understand what you mean. Perhaps you need to configure the web server serving those .js files to follow symlinks. – Basile Starynkevitch Oct 29 '11 at 11:48

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 :(

  • 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 Oct 29 '11 at 11:37

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.