Instructables

Why can't directories be hard-linked on Unix filesystems (e.g. HFS)?

On both my MacBook (OS 10.5.8) and Linux (RHEL4) I've tried to do some directory organiztion using hard links (ln, not ln -s). When I try to hard-link a directory, I get an error message:

{yakut05:81} mkdir tempo
{yakut05:83} ln tempo tempo-link
ln: `tempo': hard link not allowed for directory
{yakut05:84} 

The man page for ln doesn't specify this limitation. I wonder if any filesystem gurus out there could explain why it's not allowed? I'm sure it has to do with the inode structure, but I'd like to be clear on the details.


ganglion2 years ago
I think this is a deliberate design choice, not a bug. If hard linked directories were allowed, then you would have the possibility of creating circular loops in the filesystem structure. This make it much harder for filesystem search algorithms to produce a comprehensive list of files and directories under a certain point.

With a hard link, there is literally no difference between one link name and the other - they are both pointers to inodes - so the search algorithm would have no way of knowing which was the 'real' name and which was the link.
kelseymh (author)  ganglion2 years ago
Ah, ha! Thank you :-) I see your point about the possibility of completely invisible circular loops. With symlinks, the circularlity is explicit, and can be excluded with careful coding (find handles it, for example), but that wouldn't be the case with hard links.
Yes - that's why you can only soft link directories.

I'm glad this was useful - I found the comment from last year after looking your page, and I wasn't sure whether you would have forgotten about it or found the answer anyway.
seandogue4 years ago
(removed by author or community request)
kelseymh (author)  seandogue4 years ago
Just FYI, the stupid new editor won't automatically HTMLify URLs.  You have to paste it in, then highlight it, hit Ctrl-L, and paste it again into the box :-(

That aside, thank you much, Sean.  It sounds (paraphrasing) like hard links are dealt with almost as though they were low-level symbolic links, and that system treats directory objects differently than file objects.

Now at least I know what's going on!
(removed by author or community request)
kelseymh (author)  seandogue4 years ago
Thanks!  Yeah, I had found it, but didn't read far enough to see the answer.  Also, I see the same error on a regular Linux system, so I wasn't sure if it was Mac-specific or not.
If I just hit the link button and paste into the little box it works for me.
developer.apple.com/mac/library/technotes/tn/tn1150.html#HardLinks

L
I failed to abide by the editor and hit post-comment...got lazy didn't redo it....the shame, the mortal shame ;)
kelseymh (author)  lemonie4 years ago
Ah, I didn't know you could open the link box without some text to be used as the anchor.  What Sean did above was paste the URL into the main editor, which doesn't auto-convert it.
Yea, the new editor didn't come with instructions (as far as I know). Still knowing some HTML is handier than it used to be.

L
lemonie4 years ago
You won't have overlooked anything obvious or made any basic error. Might the permissions or path be "funny" on the target drive / directory?

L
kelseymh (author)  lemonie4 years ago
I don't think so.  The same ln command, in the same directory, works perfectly well if I apply it to a simple file.  That's why I'm confused.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!