Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do you create a hardlink (as opposed to a symlink or a Mac OS alias) in OS X that points to a directory? There is the command "ln target destination" but that only works when the target is a file.

I know that Mac OS, unlike other Unix environments, does allow hardlinking to folders (this is used for Time Machine, for example) but I don't know how to do it myself. I am also aware that using directory hardlinks is not "best practice" as it can cause some programs to recurse infinitely while scanning a tree.

share|improve this question
Note: this question ported from my SO account as more relevant for this forum – username May 9 '09 at 11:44

Here is the link to the question on Stack Overflow:

What is the Bash command to create a hardlink to a directory in OS X?

share|improve this answer
I know ;-) It's my own question. I figured it would be happier living on serverfault – username May 9 '09 at 11:50

It's supposed to be impossible.

From man 2 link:

int link(const char *path1, const char *path2);


In order for the system call to succeed, path1 must exist and both path1 and path2 must be in the same file system. As mandated by POSIX.1, path1 may not be a directory.

I tried the code in the other answer on MacOS X 10.5.6 - it didn't work when trying to create a link from a directory to the same name in the same directory.

However it does work if the two resulting directories don't have the same parent. It's necessary to read the linked article to find that out.

share|improve this answer
Apple specifically extended their link syscall to allow this behavior in limited circumstances to allow TimeMachine backups to consume less space, which still being logically complete – Dave Cheney May 9 '09 at 14:25
but haven't updated the man page to say so ;-) – Alnitak May 9 '09 at 14:50
The Open Group page ( for ln says, "If the -s option is not specified, whether a directory can be linked is implementation-defined." And my link page (on Ubuntu) doesn't say that (although this obviously doesn't work on Ubuntu). – Matthew Flaschen May 9 '09 at 19:21

I heard it was intentionally disabled, since it can get complicated when you change the hard links to . and stuff.

share|improve this answer
up vote 0 down vote accepted

You can't do it directly in BASH then. However... I found an article here that discusses how to do it indirectly: by compiling a simple little program:

#include <unistd.h>
#include <stdio.h>
main(int argc, char *argv[])
   if (argc != 3)
      return 1;
   int ret = link(argv[1], argv[2]);
   if (ret != 0)
   return ret;

$ gcc -o hlink hlink.c -Wall
share|improve this answer
This is the road to madness. – chris Jan 26 '10 at 18:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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