bash 在 OS X 中创建一个目录的硬链接的 Unix 命令是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/80875/
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
What is the Unix command to create a hardlink to a directory in OS X?
提问by username
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.
如何在 OS X 中创建指向目录的硬链接(而不是符号链接或 Mac OS 别名)?我已经知道命令“ln target destination”,但只有当目标是文件时才有效。我知道 Mac OS 与其他 Unix 环境不同,确实允许硬链接到文件夹(例如,这用于 Time Machine),但我不知道如何自己做。
采纳答案by username
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.htmlby compiling a simple little C program:
你不能直接在 BASH 中做到这一点。但是......我在这里找到了一篇文章,讨论了如何间接做到这一点:http: //www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html通过编译一个简单的小 C 程序:
#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:
...并在 Terminal.app 中构建:
$ gcc -o hlink hlink.c -Wall
回答by Bob
I agree that hard-linking folders/directories can cause problems if not careful, but they have a very definite advantage - Time Machine is a perfect example. Without them it simply would not be practical as the duplication of redundant versions of files would very quickly consume even the largest of disks.
我同意硬链接文件夹/目录如果不小心可能会导致问题,但它们有一个非常明显的优势 - Time Machine 就是一个完美的例子。没有它们,这根本不切实际,因为即使是最大的磁盘,文件的冗余版本的重复也会很快消耗掉。
Snow Leopard can create hard links to directories as long as you follow Amit Singh's six rules:
只要您遵循 Amit Singh 的六项规则,Snow Leopard 就可以创建指向目录的硬链接:
- The file system must be journaled HFS+.
- The parent directories of the source and destination must be different.
- The source's parent must not be the root directory.
- The destination must not be in the root directory.
- The destination must not be a descendent of the source.
- The destination must not have any ancestor that's a directory hard link.
- 文件系统必须记录为 HFS+。
- 源和目标的父目录必须不同。
- 源的父目录不能是根目录。
- 目标不能在根目录中。
- 目标不能是源的后代。
- 目的地不能有任何作为目录硬链接的祖先。
So it's not correct at all that Snow Leopard has lost the ability to create hard links to folders.
因此,Snow Leopard 失去了创建文件夹硬链接的能力是完全不正确的。
I just verified that link/unlink do work on Snow Leopard - as long as you follow the six rules. I just tried it and it works fine on my Snow Leopard 10.6.6 system - tried it on the boot volume and on a separate USB external volume and it worked fine in both cases.
我刚刚验证了链接/取消链接在 Snow Leopard 上确实有效 - 只要您遵循六个规则。我刚刚尝试过,它在我的 Snow Leopard 10.6.6 系统上运行良好 - 在启动卷和单独的 USB 外部卷上尝试过,在两种情况下都运行良好。
Here is the "hunlink.c" program:
这是“hunlink.c”程序:
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if (argc != 2)
return 1;
int ret = unlink(argv[1]);
if (ret != 0)
perror("unlink");
return ret;
}
gcc -o hunlink hunlink.c
So, be careful if you try it - remember to follow the rules and use hlink to create these hard links and use hunlink to remove the hard link afterwards. And don't forget to document what you've done for later on or for someone else who might need to know this.
所以,尝试时要小心——记住遵循规则并使用 hlink 创建这些硬链接,然后使用 hunlink 删除硬链接。并且不要忘记记录您为以后所做的工作或为可能需要知道这一点的其他人所做的工作。
One other "gotcha" that I just learned about these "hard links" to folders. When you create them there is really a lot that happens "behind the curtain" of Mac OS X. One really important issue is that the folder you create the link to is really moved to a super-magical super-hidden folder called /.HFS+ Private Directory Data%000d/dir_xxx where xxx is the inode number of the "source_folder" - remember the format of the command is
我刚刚了解到这些文件夹的“硬链接”的另一个“问题”。当您创建它们时,Mac OS X 的“幕后”确实发生了很多事情。一个非常重要的问题是您创建链接的文件夹确实移动到了一个名为 /.HFS+ 的超级神奇的超级隐藏文件夹中Private Directory Data%000d/dir_xxx 其中xxx是“source_folder”的inode号——记住命令的格式是
hlink source_folder target_folder
So because of this, you have to be careful of not having any files open in the "source_folder" because if you do, they just got moved to the super-magical folder and you will likely have a problem if you try and save any changes to those files that were open in the "source_folder". This happened to me a couple of times until it dawned on me what was happening and the solution is pretty simple. I noticed that you couldn't do a "ls -la" command any longer without getting funny errors for all the folders/directories that were in the original "source_folder" but you could do a "ls" command and all looked well.
因此,您必须小心不要在“source_folder”中打开任何文件,因为如果您这样做,它们就会被移动到超级神奇的文件夹中,如果您尝试保存任何更改,您可能会遇到问题到那些在“source_folder”中打开的文件。这发生在我身上几次,直到我明白发生了什么,解决方案非常简单。我注意到您不能再执行“ls -la”命令,而不会对原始“source_folder”中的所有文件夹/目录产生有趣的错误,但您可以执行“ls”命令并且一切看起来都很好。
If you run "Verify disk" in the "Disk Utility" program, you will notice that it probably complains and gives a "Volume bitmap needs minor repair for orphaned blocks" which is what just happened with the creation of the super-magical folder and the movement of the "source_folder" to it.
如果您在“磁盘工具”程序中运行“验证磁盘”,您会注意到它可能会抱怨并给出“卷位图需要对孤立块进行小修”,这正是创建超级神奇文件夹时发生的情况, “source_folder”向它的移动。
If you do find yourself in this situation with "orphaned blocks", first save the changed files to some other temporary location not in the volume containing the "source_folder" tree, then use "Disk Utility" to unmount and remount the volume that contains the "source_folder" or just restart the computer. Then copy the files you saved to the temporary locations back to their original locations and you should be back in business. This is what worked for me, so can't guarantee this will work for you too. So it might be a good idea to try this out on a volume you have a good backup of just in case.
如果您确实发现自己处于“孤立块”的这种情况,请首先将更改的文件保存到不包含“source_folder”树的卷中的其他临时位置,然后使用“磁盘工具”卸载并重新挂载包含“source_folder”或只是重新启动计算机。然后将您保存到临时位置的文件复制回其原始位置,您应该可以恢复营业了。这对我有用,所以不能保证这对你也有用。因此,为了以防万一,在您有良好备份的卷上尝试此操作可能是个好主意。
It seems so very weird that all this overhead occurs just for the simple task of creating a hard link to a folder. Does anyone have any idea why Mac OS X goes to all this effort for this hard link creation to folders? Does it have something to do with the fact that this is a "journaled" file system?
看起来很奇怪,所有这些开销只是为了创建一个到文件夹的硬链接的简单任务。有谁知道为什么 Mac OS X 为创建文件夹的硬链接而付出所有这些努力?它与这是一个“日志式”文件系统的事实有关吗?
I discovered the info about the super-magical, super-hidden location by reading Amit Singh's explanation of his "hfsdebug" utility. If you want more details see his web site at Amit Singh's hfsdebug utility. It's a very interesting piece of software and will tell you lots of details about HFS+ file systems. It's free and I encourage you to download it and try it out. It's no longer supported but it still works on both Snow Leopard and Leopard - basically any HFS+ supported system. You can't really do any harm with it as it's a "read-only" tool - so it's great to use to look at some details of the filesystem.
通过阅读 Amit Singh 对其“hfsdebug”实用程序的解释,我发现了有关超级神奇、超级隐藏位置的信息。如果您想了解更多详细信息,请参阅他的网站Amit Singh 的 hfsdebug 实用程序。这是一个非常有趣的软件,它会告诉你很多关于 HFS+ 文件系统的细节。它是免费的,我鼓励您下载并试用。它不再受支持,但它仍然适用于 Snow Leopard 和 Leopard——基本上是任何 HFS+ 支持的系统。由于它是一个“只读”工具,因此您不会真正对它造成任何伤害 - 因此它非常适合用于查看文件系统的一些细节。
One more issue about these "hard links to folders" - once you create one and the super-magical super-secret-hidden folder gets created, it's there for good. Even if you unlink the folder that caused it to be created in the first place, this magic folder stays around. Not sure why, but it definitely does. You can use "hfsdebug" to find this out if you wish to try it out. You can also use "hfsdebug" to find out how many of these "hard links to folders" exist on a drive. For these details refer to Amit's article on the "hfsdebug" utility.
关于这些“文件夹硬链接”的另一个问题——一旦你创建了一个文件夹并且超级神奇的超级秘密隐藏文件夹被创建,它就永远存在。即使您取消链接导致它首先被创建的文件夹,这个神奇的文件夹仍然存在。不知道为什么,但确实如此。如果您想尝试一下,可以使用“hfsdebug”来找到它。您还可以使用“hfsdebug”来找出驱动器上存在多少这些“文件夹硬链接”。有关这些详细信息,请参阅 Amit 关于“hfsdebug”实用程序的文章。
He also has another newer utility that's supported but costs. It's called fileXray and costs $79 for one person on any number of computers in the same household for a personal non-business type license. It has an extensive 173-page User Guide that you can download to see what it can do before you purchase. Unfortunately there is no trial version, so read the manual and check out the web site for more details to see if it can help you out of a jam. Learn all the details about it at their web site - see fileXray web sitefor more info.
他还有另一个受支持但成本较高的较新实用程序。它被称为 fileXray,一个人在同一个家庭中使用任意数量的计算机的费用为 79 美元,以获得个人非商业类型的许可证。它有一个广泛的 173 页用户指南,您可以在购买之前下载它以查看它的功能。不幸的是,没有试用版,因此请阅读手册并查看网站以了解更多详细信息,看看它是否可以帮助您摆脱困境。在他们的网站上了解有关它的所有详细信息 -有关更多信息,请参阅fileXray 网站。
There are a couple of issues you should be aware of when using these hard links to folders. If the volume that they are created on is mounted to a remote client, there can be significant problems, depending on how they are mounted. If you use AFP to mount the volume to a remote client, there are big problems as any folder that currently has a hard link to it or has ever had one but later removed, will be unable to be used as all the lower level folders (but not files) will be inaccessible from either the Finder or a Terminal window. If you try to do a simple "ls -lR" command, it will fail and give you "ls: xxx: No such file or directory" error messages for all lower level folders. If you use a Finder window to traverse the directory tree of the remote volume, the folders that are in the folder that had or has a hard link to it will simply disappear without any error when you first click on the folder name.
使用这些指向文件夹的硬链接时,您应该注意几个问题。如果创建它们的卷挂载到远程客户端,则可能会出现严重问题,具体取决于它们的挂载方式。如果您使用 AFP 将卷挂载到远程客户端,则会出现大问题,因为当前有硬链接或曾经有但后来被删除的任何文件夹将无法用作所有较低级别的文件夹(但不是文件)将无法从 Finder 或终端窗口访问。如果您尝试执行简单的“ls -lR”命令,它将失败并为您提供所有较低级别文件夹的“ls:xxx:没有此类文件或目录”错误消息。如果您使用 Finder 窗口遍历远程卷的目录树,
These problems don't appear to occur (except for the error message) if you use NFS to mount the remote client (and assuming you had a NFS server on the system that has the volume as a local HFS+ filesystem). Details on how to use NFS to mount volumes are not provided here. I used a nice program from Dr. Marcel Bresink called "NFS Manager" to help with the NFS mounts on the server and client. You can get it from his web site - just search for "Bresink NFS Manager" in your favorite search engine, but he has a free trial version so you can try before you buy. It's not that big a deal if you want to learn how to do the NFS mounts, but the "NFS Manager" makes it pretty easy to set things up and to tweak all the different settings to help optimize it. He has several other neat Mac OS X utilities too that are very reasonably priced - one called "Hardware Monitor" that lets you monitor and graph all kinds of things like power usage, temperature of CPU, speed of fans and many many other variables for both the local and remote Mac systems over extended periods of time (from minutes to days). Definitely worth checking out if you are into handy utilities.
如果您使用 NFS 挂载远程客户端(并假设您的系统上有一个 NFS 服务器,该服务器将卷作为本地 HFS+ 文件系统),则这些问题似乎不会发生(错误消息除外)。此处未提供有关如何使用 NFS 挂载卷的详细信息。我使用了 Marcel Bresink 博士提供的一个名为“NFS Manager”的不错的程序来帮助在服务器和客户端上安装 NFS。你可以从他的网站上得到它——只需在你最喜欢的搜索引擎中搜索“Bresink NFS Manager”,但他有一个免费试用版,所以你可以在购买前试用。如果您想学习如何进行 NFS 挂载,这并不是什么大不了的事,但是“NFS 管理器”使设置和调整所有不同设置以帮助优化它变得非常容易。他还有其他几种简洁的 Mac OS X 实用程序,它们的价格非常合理 - 一种称为“硬件监视器”,可让您监视和绘制各种事物,例如电源使用情况、CPU 温度、风扇速度和许多其他变量长时间(从几分钟到几天)的本地和远程 Mac 系统。如果您喜欢方便的实用程序,绝对值得一试。
One thing I did notice is that NFS file transfers were about 20% slower than doing them via AFP, but your "mileage may vary", so no guarantees one way or the other, but I would rather have something that works even if I have to pay a 20% performance hit as compared to having nothing work at all.
我注意到的一件事是,NFS 文件传输比通过 AFP 传输大约慢 20%,但是您的“里程可能会有所不同”,因此无法保证以一种或另一种方式进行,但我宁愿拥有一些有效的东西,即使我有与根本没有任何工作相比,付出了 20% 的性能损失。
Apple is aware of the problems with hard links and remote AFP filesystems, and they refer to it as an "implentation limitation" of the AFP client - I prefer to call it what it really appears to me to be - A BUG!!! I can only hope the next release of Mac OS X fixes the problem, as I really like having the ability to use hard links to folders when it makes sense.
Apple 意识到硬链接和远程 AFP 文件系统的问题,他们将其称为 AFP 客户端的“实施限制”——我更喜欢称其为我认为的真实情况——一个 BUG!!!我只能希望 Mac OS X 的下一个版本能解决这个问题,因为我真的很喜欢能够在有意义的时候使用文件夹的硬链接。
These notes are my own personal opinion and I don't make any warranty about their correctness so use them at your own risk. Have a good backup before you play around with these "hard links to folders" just in case something unforeseen happens. But I hope you have fun if you do decide to look a bit more into this interesting aspect of Mac OS X.
这些笔记是我个人的意见,我不对它们的正确性做任何保证,因此使用它们的风险自负。在使用这些“文件夹硬链接”之前,请做好备份,以防万一发生意外。但是,如果您决定更深入地研究 Mac OS X 的这个有趣方面,我希望您能玩得开心。
回答by Rich
Piffle. On 10.5, it tells you in the man page for ln:
废话。在 10.5 上,它在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 mustdocument 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!
那里。希望你不要破坏你的文件系统!
回答by Simon East
Cross-posting this great toolwhich neatly solves the problem, originally posted by Sam:
交叉发布这个很好的工具,它巧妙地解决了这个问题,最初由Sam发布:
To install Hardlink, ensure you've installed homebrew, then run:
要安装 Hardlink,请确保您已安装homebrew,然后运行:
brew install hardlink-osx
Once installed, create a hard link with:
安装后,创建一个硬链接:
hln [source] [destination]
I also noticed that unlink
command does not work on snow leopard, so I added an option to unlink:
我还注意到该unlink
命令对雪豹不起作用,所以我添加了一个取消链接的选项:
hln -u destination
Code is available on Github for those who are interested: https://github.com/selkhateeb/hardlink
有兴趣的人可以在 Github 上获得代码:https: //github.com/selkhateeb/hardlink
回答by Simon East
Yes it's supported by the kernel and the filesystem, but since it's not intended for general usage it's not exposed to the shell.
是的,内核和文件系统都支持它,但由于它不用于一般用途,因此它不会暴露给外壳。
You could probably work out which APIs Time Machine uses and wrap them in a commandline tool, but it'd be better to take the hint and steer well-clear.
您可能可以计算出 Time Machine 使用哪些 API 并将它们包装在命令行工具中,但最好接受提示并明确引导。
回答by ccpizza
The OSX version of ln
cannot do it, but, as mentioned in the other answer by rich, it is possible with the GNU version of ln
which is available in homebrewas gln
as part of the coreutilsformula. man gln
lists the -d
option with the OSX-specific warning provided in rich's answer. In other words, it does not work in all cases. What exactly determines whether it works or not does not seem to be documented anywhere.
OSX 版本ln
无法做到这一点,但是,正如在rich的另一个答案中提到的,它的GNU 版本ln
可以作为coreutils公式的一部分在自制软件gln
中使用。列出了在Rich的回答中提供的特定于 OSX 的警告的选项。换句话说,它并不适用于所有情况。究竟是什么决定了它是否有效,似乎在任何地方都没有记录。man gln
-d
As a prerequisite, install coreutils
:
作为先决条件,安装coreutils
:
brew install coreutils
Now you can do:
现在你可以这样做:
sudo gln -d /original_folder /mirror_folder
IMPORTANT: To remove the hard link you mustuse gunlink
:
重要提示:要删除硬链接,您必须使用gunlink
:
sudo gunlink /mirror_folder
?????? Using
rm
or Finder will also delete the original folder.
??????使用
rm
或 Finder 也会删除原始文件夹。
FYI: The coreutilshomebrew formula provides the GNU-compatible versions of generic unix tools. Use brew list coreutils
to see the full list.
仅供参考:coreutilshomebrew 公式提供了通用 unix 工具的 GNU 兼容版本。使用brew list coreutils
以查看完整列表。
回答by ariel
As of 2018 no longer possible. APFS (introduced in MacOS High Sierra 10.13) is not compatible with directory hardlinks. See https://github.com/selkhateeb/hardlink/issues/31
截至 2018 年,不再可能。APFS(在 MacOS High Sierra 10.13 中引入)与目录硬链接不兼容。见https://github.com/selkhateeb/hardlink/issues/31
回答by Jesper R?nn-Jensen
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.
我的情况是我发现从 Windows 虚拟机中,我无法遵循符号链接。(我想在 Internet Explorer 中测试一些 HTML 页面)。我的目录结构有 CSS 和图像文件夹的符号链接。
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.
我解决问题的方法与暗示的其他答案不同。我曾经rsync
创建了该文件夹的副本。Rsync 可以解析符号链接并复制链接的文件。
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/
回答by Penfold
The short answer is you can't. :) (except possibly as root, when it would be more accurate to say you shouldn't.)
简短的回答是你不能。:)(除非可能是 root,否则更准确地说你不应该这样做。)
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.
Unix 只允许一定数量的目录链接 - 来自其所有子目录中的“..”和“.” 从自身内部。其他任何东西都可能是一个非常混乱的目录树的秘诀。这显然是/曾经是 Ken Thompson 的设计决定。
(Having said that, apparently Apple's Time Machine does do this :) )
(话虽如此,显然 Apple 的 Time Machine 确实做到了这一点 :))
回答by Gannet
From the article linked to, you'll get that error if you try to create the hard link in the same directory as the original. You have to create it somewhere else.
从链接到的文章中,如果您尝试在与原始目录相同的目录中创建硬链接,您将收到该错误。您必须在其他地方创建它。