macos 在 Mac OS X 中创建目录硬链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1432540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating directory hard links in Mac OS X
提问by Felix Geisend?rfer
How can I create a hard link to a directory in Mac OS X?
如何在 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.
此功能已添加到他们在Mac OS X v10.5(Leopard)(用于时间机器)的文件系统中,但我无法从命令行找到有关实际使用它的任何信息。
回答by Sam
I have bundled up the suggested answer in a Git repository if anybody is interested: https://github.com/selkhateeb/hardlink
如果有人感兴趣,我已将建议的答案捆绑在 Git 存储库中: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:
我还注意到该unlink
命令在 Mac OS X v10.6 (Snow Leopard) 上不起作用,所以我添加了一个取消链接的选项:
hln -u destination
To install Hardlink, use Homebrewand run:
要安装 Hardlink,请使用Homebrew并运行:
brew install hardlink-osx
回答by Freeman
Unfortunately Apple has crippled the ln
command. You can use the following program to create a hard link to a directory:
不幸的是,Apple 削弱了该ln
命令。您可以使用以下程序创建指向目录的硬链接:
#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
回答by rleber
In the answer to the question by the_undefined about how to remove a hardlink to a directory without removing the contents of other directories to which it is linked: As far as I can tell, it can't be done from the command line using builtin commands. However, this little program (inspired by Freeman's post) will do it:
在 the_undefined 关于如何删除指向目录的硬链接而不删除它所链接到的其他目录的内容的问题的答案中:据我所知,无法使用内置命令从命令行完成. 但是,这个小程序(受 Freeman 的帖子启发)可以做到:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr,"Use: hunlink <dir>\n");
return 1;
}
int ret = unlink(argv[1]);
if (ret != 0)
perror("unlink");
return ret;
}
To follow on with Freeman's example,
继续以弗里曼为例,
$ gcc hunlink.c -o hunlink
$ echo "foo bar" > child1/baz.txt
$ ./hunlink parent/clone2
will remove the hardlink at parent/clone2, but leave the directory child1
and the file child1/baz.txt
alone.
将删除 parent/clone2 上的硬链接,但保留目录child1
和文件child1/baz.txt
。
回答by Kit Sunde
Another solution is to use bindfs https://code.google.com/p/bindfs/which is installable via port:
另一种解决方案是使用可以通过端口安装的bindfs https://code.google.com/p/bindfs/:
sudo port install bindfs
sudo bindfs ~/source_dir ~/target_dir