Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can hard links get broken?

Tags:

git

bash

unix

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)
like image 868
Jan Avatar asked Nov 19 '13 11:11

Jan


People also ask

What are the limitations of hard links?

Hard limits While useful, there are some limitations to what hard links can do. For starters, they can only be created for regular files (not directories or special files). Also, a hard link cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.

Should I use hard or soft links?

In the end, the difference between hard links and soft links is pretty simple. Hard links are more forgiving when you delete a file, soft links take up less data because it's just pointing the way. However, soft links don't store the actual data, they just store the location of the original file.

What will happen if the hard link is removed?

If the hard link gets deleted, nothing happens to the original file, as it still has its data in the hard drive. Vice versa, if the original file gets deleted, the hard link still refers to the location of the data in the hard drive, therefore the information is still saved.

Should I use hard links?

If you need to have a file on more that one place in your filesystem, or your original file is getting moved around, or if it is a big file that you need to work quickly, a hard link is good to use.


2 Answers

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?

like image 151
Snowball Avatar answered Oct 20 '22 11:10

Snowball


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
}
like image 39
carlo Avatar answered Oct 20 '22 10:10

carlo