Can hard links get broken?

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

  • Use Git / Github to sync my Projects between PC / Laptop / WebInterface
  • How do you set default application for `open` command in terminal based on file extension type?
  • Shell script self update using git
  • how to start bash in a directory in windows .bat?
  • git with --git-dir= results in 'not a git repository'
  • Error 0x2 starting node.exe -v in Git bash for Windows
  • 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)

  • How can I get last commit from GitHub API
  • Git command to show author, comment and lines modified
  • Hide VSTS (VisualStudio.com) GIT Branches
  • How does the shell script envsetup.sh execute launch?
  • What is the git equivalent for a specific version checkout, like “svn checkout -r 123 <svn_rep_url>”
  • Jenkins loses workspaces periodically, triggers unwanted build
  • 2 Solutions collect form web for “Can hard links get broken?”

    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?

    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
    }
    
    Git Baby is a git and github fan, let's start git clone.