8

I have the feeling that from time to time my hard links get broken.

I use to synchronize a couple of copies by creating links via, e.g.:

link ~/work/genDocs/bibs/SKM.bib SKM.bib

Once a while I recognize that a synchronization hasn't taken place and I "renew" the link. Personally I don't think, this should happen, but may it be that such links get broken?

Reasons I can think of:

  • system updates
  • interference with version control (I use git)
Jan
  • 4,932
  • 1
  • 26
  • 30

2 Answers2

14

This can happen if the original file (~/work/genDocs/bibs/SKM.bib) is recreated instead of being modified in-place. A new inode will be created, but your link will still point to the old inode. You can fix the issue by creating symbolic links with ln -s instead of hard links with link. See What is the difference between a symbolic link and a hard link?

Community
  • 1
  • 1
Snowball
  • 11,102
  • 3
  • 34
  • 51
2

To avoid inode changes when modifying the contents of files there is the Unix text file editor ed.

Although (almost) all ed implementations make use of temporary files as well (see "In-place" editing of files), ed - unlike sed -i (as already pointed out by chepner) - modifies files "in-place" without changing their respective inodes (see Editing files with the ed text editor from scripts).

# sed & ed example to demonstrate whether inode is being changed or preserved

# sed
# inode is changed
{
rm -Pfv testfile.txt
echo a > testfile.txt
ls -i testfile.txt
sed -i -e 's/a/A/' testfile.txt
ls -i testfile.txt
}

# ed
# inode is not changed
{
rm -Pfv testfile.txt
echo a > testfile.txt
ls -i testfile.txt
printf '%s\n' 'H' ',s/a/A/' 'wq' | ed -s testfile.txt
ls -i testfile.txt
}

# > 
# redirection operator preserves inode (on Mac OS X 10.6.8)
{
rm -Pfv testfile.txt
echo a > testfile.txt
ls -i testfile.txt
echo A > testfile.txt
ls -i testfile.txt
}
carlo
  • 21
  • 1