1

I know how hard links and symlinks work and I know why hard links can't be used for directories but in this case, is it some kind of exception?

For example I do:

ls -al Documents

total 8
drwxr-xr-x  2 piotr piotr 4096 cze 28 11:19 .
drwxrwx--- 17 piotr piotr 4096 lip  2 16:41 ..

. is a hard link to Documents itself and .. is a hard link to my home directory so hey, it's illegal

2
  • 2
    hard links to directories are not "illegal". They are standard. That the user cannot create arbitrary hard links (e.g. with ln) is not the same as saying they may not exist. Jul 2 at 15:12
  • macOS (a certified UNIX system) used to use hard links to directories for its builtin backup software, but they weren't creatable via ln
    – Fox
    Jul 2 at 15:49
3

As someone said in a comment on the question, just because hard links to directories aren't permitted (i.e., by the ln command), does not mean they are not possible. The superuser can actually use the "-d" or "-F" option to the ln command to force the creation of a hard link to a directory (though the man page says it will "probably" fail due to filesystem restrictions - not sure what that's about, and I'm not going to try it on one of my own systems to see...).

Hard links to directories are not permitted because they can create loops for programs that try to traverse the directory structure. In any directory, . and .. are hard links to that directory, and its parent, respectively - these are "well known" special cases and anything that tries to traverse the filesystem knows to account for that. But it is certainly technically possible to create a hard link to a directory if you're persistent - it's just not advisable.

1
  • it can fail due to filesystem restrictions because not every file system supports hard links
    – phuclv
    Jul 3 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.