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:
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.
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With