When would creating a hard link be useful?

Hardlinks help us organize our file system in a much more flexible way. Basically, hardlinks allow us to take one file and have it be multiple places in the filesystem at once. Think about a scenario where you are a photographer and have lots of photos (this is an example from my life!). You might organize them by the people that appear in them, because sometimes people ask you for photos of them. But you might also want to organize them by the location and by the date. There's no real way to nest these three things, they're totally separate axes of organization. So you can create three different hierarchies for these three different things, and have each photo present in all three, without having to store each photo three times. That's the magic of hardlinks. Unlink symlinks, we don't need to worry about where the "real file" is, because they're all the real file. We can delete and move at will, because the file will be retained until there are no longer any references to it, and removed when you delete the last hardlink. It's simple and doesn't require you to keep track of very much.


A file's contents will not be purged until all hard links (yes, all filenames are hard links, even the first) have been erased and the file closed. As such, it can be useful when a file is required in multiple places, but may be removed from any of them at any time, e.g. between ~/Downloads/coolsong.mp3 and ~/Music/Cool Song.mp3.

Tags:

Linux

Unix