5

I am using Mac OS X, but the command line. I want to make a link from my .profile file, to another file on my system so that updating one updates the other and vice versa.

This article makes me think that a hard link is what I need. The command I have been using is:

ln .profile ~/Newpath/.profile

This kind of works, in that a file is created at Newpath, however, updating one file does not automatically update the other nor vice versa.

I have tried ln with simple files on my desktop, and the links do indeed update each other. I am wondering if anybody has experience with links not working with dot files or with files in their home directory on Mac for some reason.

Any idea what could be going on here?

5 Answers 5

15

Beware that many editors write to a temporary file and rename finally to the target file.

This will cause your hard-linking to be lost.

1
  • 4
    What do you do about this? This seems to be my case. Feb 2, 2017 at 16:29
3

Perhaps the directories where these files reside are on different filesystems / partitions? Hard links can only exist on a single partition.

if test x"$(stat -f'%d' .)" != x"$(stat -f'%d' ~/Newpath)"; then
  echo "Directories are on different partitions"
fi
3
  • 3
    If they were different filesystems, the ln command would have failed. Jun 14, 2016 at 15:35
  • Yes, @StéphaneChazelas is correct.
    – dubiousjim
    Jun 14, 2016 at 17:35
  • This is not the case in the question, since it works for the first time but not after updates. Jul 14, 2016 at 15:29
1

I just tested it on my Mac, it worked perfectly well.
Updating either of them, the other one would be updated too.

BTW, symbolic link should work as well as hard link.

4
  • 1
    I am using hard links because I wanted to use git with these files. I added clarification to my question because the linking does indeed work on simple text files, on my desktop for example but not with these dot files in my home directory.
    – Startec
    Sep 25, 2015 at 5:45
  • 7
    I think git will break hard links every time you checkout a new copy of the file. EDIT: Yes, I just verified it will, even if the hard links are in a single repo.
    – dubiousjim
    Sep 25, 2015 at 7:07
  • This is actually the real culprit I believe.
    – Startec
    Aug 5, 2016 at 7:53
  • Git should work reasonably well with symbolic links, though. Even if the underlying filesystem doesn't support them (e.g., FAT32), git will mimic them via text files (but you won't be able to use them).
    – leden
    Sep 28, 2016 at 1:18
0

I also faced the same issue when writing with TextEdit. So then, I tried with cat command and it worked perfectly.

2
  • You tried what with a cat command? Aug 1, 2017 at 18:26
  • I gave input to the file using cat command instead of writing through some editor. Aug 2, 2017 at 5:28
0

dubiousjim's comment pointed out my issue:

I think git will break hard links every time you checkout a new copy of the file. EDIT: Yes, I just verified it will, even if the hard links are in a single repo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .