macosbashfilesystemsunixln

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


How do you create a hardlink (as opposed to a symlink or a Mac OS alias) in OS X that points to a directory? I already know 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.


Solution

  • You can't do it directly in BASH then. However... I found an article here that discusses how to do it indirectly: http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html by compiling a simple little C program:

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

    ...and build in Terminal.app with:

    $ gcc -o hlink hlink.c -Wall