I understand the notion of hardlinks very well, and have read the man pages for basic tools like cp
--- and even the recent POSIX specs --- a number of times. Still I was surprised to observe the following behavior:
$ echo john > john
$ cp -l john paul
$ echo george > george
At this point john
and paul
will have the same inode (and content), and george
will differ in both respects. Now we do:
$ cp george paul
At this point I expected george
and paul
to have different inode numbers but the same content --- this expectation was fulfilled --- but I also expected paul
to now have a different inode number from john
, and for john
to still have the content john
. This is where I was surprised. It turns out that copying a file to the destination path paul
also has the result of installing that same file (same inode) at all other destination paths that share paul
's inode. I was thinking that cp
creates a new file and moves it into the place formerly occupied by the old file paul
. Instead what it seems to do is to open the existing file paul
, truncating it, and write george
's content into that existing file. Hence any "other" files with the same inode get "their" content updated at the same time.
Ok, this is a systematic behavior and now that I know to expect it I can figure out how to work around it, or take advantage of it, as appropriate. What puzzles me is where I was supposed to see this behavior documented? I'd be surprised if it's not documented somewhere in documents I've already looked at. But apparently I missed it, and can't now find a source that discusses this behavior.