Editing a file with several hard links

hard link

Let's say I have two hard links pointing at the same picture.

/photography/picture_1.jpg
/best_pictures/picture_1.jpg

What happens if I edit /photography/picture_1.jpg? Is the hard link broken and did I end up with 2 different files? Does it keep the link and therefore edit the "second" file, accessed by the second pointer?

Best Answer

A hard link is simply an alternative name for the same inode (file). Editing the file found at either of those paths will change the picture that both paths point to.

A soft/symbolic link is different: it's a pointer to the original file and can be broken. A hard link is not a pointer to the file, it is the same file under a different name.

However, some editing tools may use temporary files (as opposed to true, in-place editing) to create and save your edits. So it may end up being dependent on the tool you use. You can experiment with your editor of choice and see if it changes a file's inode number after editing. Find out a file's inode number from the output of ls -i filename (Thanks to Sparhawk's comment for that note).

See also:

Related Question