Shell Hard Link – Forcibly Create Directory Hard Links

hard linklnshell

I understand the reasoning why nearly every unix version doesn't allow hard-linking of directories (in fact HFS+ on OS X is the only one I know, but even that isn't made easy to do yourself). However, all file-systems in theory support hard-linked directories, as all directories contain at least one extra hard-link to itself, plus extra hard-links in sub-directories pointing back to their parent.

Now, I realise that hard-linking can be dangerous if misused, as it can create cyclical structures that few programs will check for, and thus become stuck in an infinite loop. However, I was hoping to use hard-links to create a Time Machine style backup that can work for any unix. I don't believe that this kind of structure would be dangerous, as the links simply point to previous backups; there should be no risk of cyclical linking. In my case I'm currently just using rsync to create hard-links to existing files, but this slow and wasteful, particularly with very large backups and especially if I already know which directories are unchanged.

With this in mind, is there any way to force the creation of directory hard-links on unix variants? ln is presumably no good as this is the place that many unix flavours put their restricts upon in order to prevent hard-linking directories, and ln versions that support hard-linked directories specifically state that the operation is likely to fail. But for someone who knows the risks, and knows that their use-case is safe, is there any way to actually create the link anyway? Ideally for a shell script, but if I need to compile a small program to do it then I suppose I could.

Best Answer

Don't do this. If you want to have a backup system using hard links to save space, better to use rsync with --link-dest, which will hard link files appropriately to save space, without causing the problems that this causes (that is, hard linking between directories is a corruption of the filesystem, and will cause it to report wrong inode counts + fail fsck + generally have unknown semantics due to not being a DAG).

Related Question