Search code examples
macoshardlink

Creating directory hard links in Mac OS X


How can I create a hard link to a directory in Mac OS X?

This feature has been added to their file system in Mac OS X v10.5 (Leopard) (for time machine), but I could not find any information on actually using it from the command line.


Solution

  • Unfortunately Apple has crippled the ln command. You can use the following program to create a hard link to a directory:

    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[]) {
     if (argc != 3) {
      fprintf(stderr,"Use: hlink <src_dir> <target_dir>\n");
      return 1;
     }
     int ret = link(argv[1],argv[2]);
     if (ret != 0)
      perror("link");
     return ret;
    }
    

    Take into account that the hard linked directories may not be in the same parent directory, so you can do this:

    $ gcc hlink.c -o hlink
    $ mkdir child1
    $ mkdir parent
    $ ./hlink child1 parent/clone2