What is the Unix command to create a hardlink to a

2019-01-10 02:30发布

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.

14条回答
Bombasti
2楼-- · 2019-01-10 03:02

My case was that I found out that from a windows virtual machine, I cannot follow symlinks. (i wanted to test some HTML pages in Internet Explorer). And my directory structure had symlinks for CSS and images folders.

My workaround to solve the problem was a different approach than the other answers implied. I used rsync to create a copy of the folder. Rsync can resolve the symlinks and copy the linked files in stead.

This solved my problem without using hard links to directories. And it's actually an easy solution if you're just working on a small set of files.

rsync -av --copy-dirlinks --delete ../htmlguide ~/src/
查看更多
甜甜的少女心
3楼-- · 2019-01-10 03:06

Piffle. On 10.5, it tells you in the man page for ln:

   -d, -F, --directory
          allow the superuser to attempt to hard link  directories  (note:
          will  probably  fail  due  to  system restrictions, even for the
          superuser)

So yes:

    sudo  ln  -d  existing_dir  new_hard_link

Give it your password, and you're not done yet. You didn't document it, did you? You must document hard linked directories; even if it's a single user machine.

Deleting is a different story: if you go about it the usual way to delete directories, you'll delete the contents. So you must "unlink" the directory:

    unlink  new_hard_link

There. Hope you don't wreck your filesystem!

查看更多
Anthone
4楼-- · 2019-01-10 03:08

The short answer is you can't. :) (except possibly as root, when it would be more accurate to say you shouldn't.)

Unixes only allow a set number of links to directories - ".." from within all its children and "." from within itself. Anything else is potentially a recipe for a very confused directory tree. This is/was apparently a design decision by Ken Thompson.

(Having said that, apparently Apple's Time Machine does do this :) )

查看更多
你好瞎i
5楼-- · 2019-01-10 03:09

In Linux you can use bind mount to simulate hard linking directories. Not sure about OSX

sudo mount --bind /some/existing_real_contents /else/dummy_but_existing_directory
sudo umount /else/dummy_but_existing_directory
查看更多
Ridiculous、
6楼-- · 2019-01-10 03:12

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
查看更多
Explosion°爆炸
7楼-- · 2019-01-10 03:15

Another solution is to use bindfs https://code.google.com/p/bindfs/ which is installable via port:

sudo port install bindfs
sudo bindfs ~/source_dir ~/target_dir
查看更多
登录 后发表回答