1699Views9Replies

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.

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
ganglion
ganglion

Best Answer 9 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.

0
kelseymh
kelseymh

Answer 9 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.

0
ganglion
ganglion

Answer 9 years ago

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.

0
kelseymh
kelseymh

11 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.

0
kelseymh
kelseymh

11 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!

0
seandogue
seandogue

Answer 11 years ago

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

0
kelseymh
kelseymh

Answer 11 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.

0
lemonie
lemonie

Answer 11 years ago

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

0
lemonie
lemonie

11 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

0
kelseymh
kelseymh

Answer 11 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.