Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating directory hard links in Mac OS X [duplicate]

Tags:

macos

hardlink

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.

like image 246
Felix Geisendörfer Avatar asked Sep 16 '09 12:09

Felix Geisendörfer


People also ask

Can you create multiple hard links to the same file?

While useful, there are some limitations to what hard links can do. For starters, they can only be created for regular files (not directories or special files). Also, a hard link cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.

How do I create a hard link in Mac OS X?

Press Command+Space, type “Terminal”, and then press “Enter” to open Terminal from Spotlight search. Navigate to Finder > Applications > Utilities > Terminal to launch the Terminal shortcut. The -s here tells the ln command to create a symbolic link. If you want to create a hard link, you'd omit the -s .

Why do directories have 2 hard links?

1 Answer. Every directory has a link to itself and its parent (that's why . of an empty directory will have a link count of 2). But because every directory links to its parent, any directory that has a subdirectory will have a link from that child.

Can hard links be created for directories?

The reason hard-linking directories is not allowed is a little technical. Essentially, they break the file-system structure. You should generally not use hard links anyway. Symbolic links allow most of the same functionality without causing problems (e.g ln -s target link ).


2 Answers

I have bundled up the suggested answer in a Git repository if anybody is interested: https://github.com/selkhateeb/hardlink

Once installed, create a hard link with:

hln source destination 

I also noticed that unlink command does not work on Mac OS X v10.6 (Snow Leopard), so I added an option to unlink:

hln -u destination 

To install Hardlink, use Homebrew and run:

brew install hardlink-osx 
like image 162
Sam Avatar answered Sep 21 '22 21:09

Sam


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 
like image 21
Freeman Avatar answered Sep 22 '22 21:09

Freeman