1469Views9Replies

Author Options:

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

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.

11 Replies

user
ganglionBest Answer (author)2011-08-27

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.

Select as Best AnswerUndo Best Answer

user
kelseymh (author)ganglion2011-08-27

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.

Select as Best AnswerUndo Best Answer

user
ganglion (author)kelseymh2011-08-27

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.

Select as Best AnswerUndo Best Answer

user
kelseymh (author)2010-03-11

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.

Select as Best AnswerUndo Best Answer

user
kelseymh (author)2010-03-10

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!

Select as Best AnswerUndo Best Answer

user
lemonie (author)kelseymh2010-03-10

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

Select as Best AnswerUndo Best Answer

user
seandogue (author)lemonie2010-03-11

I failed to abide by the editor and hit post-comment...got lazy didn't redo it....the shame, the mortal shame ;)

Select as Best AnswerUndo Best Answer

user
kelseymh (author)lemonie2010-03-11

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.

Select as Best AnswerUndo Best Answer

user
lemonie (author)kelseymh2010-03-11

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

Select as Best AnswerUndo Best Answer

user
lemonie (author)2010-03-10

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

Select as Best AnswerUndo Best Answer

user
kelseymh (author)lemonie2010-03-10

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.

Select as Best AnswerUndo Best Answer