在 OS X 中创建目录硬链接的 Unix 命令是什么?
如何在 OS X 中创建指向目录的硬链接(而不是符号链接或 Mac OS 别名)? 我已经知道命令“ln target destination”,但仅当目标是文件时才有效。 我知道 Mac OS 与其他 Unix 环境不同,确实允许硬链接到文件夹(例如,这用于 Time Machine),但我自己不知道如何做到这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我同意硬链接文件夹/目录如果不小心可能会导致问题,但它们有一个非常明确的优势 - 时间机器就是一个完美的例子。 如果没有它们,它根本就不实用,因为文件的冗余版本的重复甚至会很快消耗最大的磁盘。
Snow Leopard 可以创建目录的硬链接,只要您遵循 Amit Singh 的六个规则:
因此,雪豹已经失去了创建硬链接的能力是完全不正确的
文件夹。
我刚刚验证了链接/取消链接在 Snow Leopard 上确实有效 - 只要您遵循六个
规则。 我刚刚尝试过,它在我的 Snow Leopard 10.6.6 系统上运行良好 - 在启动卷和单独的 USB 外部卷上尝试过,在两种情况下都运行良好。
这是“hunlink.c”程序:
因此,如果您尝试它,请小心 - 记住遵循规则并使用 hlink 创建这些硬链接,然后使用 hunlink 删除硬链接。 并且不要忘记记录
您以后所做的事情或为其他可能需要知道这一点的人所做的事情。
我刚刚了解到这些文件夹的“硬链接”的另一个“陷阱”。 当您创建它们时,Mac OS X 的“幕后”确实发生了很多事情。一个非常重要的问题是您创建链接的文件夹实际上被移动到了一个名为 /.HFS+ 的超级神奇的超级隐藏文件夹Private Directory Data%000d/dir_xxx 其中 xxx 是“source_folder”的 inode 号 - 请记住命令的格式是
因此,您必须小心不要在“source_folder”中打开任何文件,因为如果您这样做,它们刚刚被移动到超级神奇的文件夹,如果您尝试保存对“source_folder”中打开的文件的任何更改,您可能会遇到问题。 这种情况在我身上发生过几次,直到我明白发生了什么,而且解决方案非常简单。 我注意到,您无法再执行“ls -la”命令,而不会在原始“source_folder”中的所有文件夹/目录中出现有趣的错误,但您可以执行“ls”命令,并且一切看起来都很好。
如果您在“磁盘实用程序”程序中运行“验证磁盘”,您会注意到它可能会抱怨并给出“卷位图需要对孤立块进行小修”,这就是创建超级神奇文件夹时发生的情况,并且将“source_folder”移动到它。
如果您确实发现自己处于这种“孤立块”的情况,请首先将更改的文件保存到不在包含“source_folder”树的卷中的其他临时位置,然后使用“磁盘工具”卸载并重新装载包含该文件的卷。 “source_folder”或只是重新启动计算机。 然后将您保存到临时位置的文件复制回其原始位置,您就可以恢复正常工作了。 这对我有用,所以不能保证这也对你有用。 因此,在您拥有良好备份的卷上尝试此操作可能是一个好主意,以防万一。
看起来很奇怪,所有这些开销只是为了创建文件夹硬链接的简单任务而发生的。 有谁知道为什么 Mac OS X 会付出如此多的努力来创建文件夹的硬链接? 这与这是一个“日志式”文件系统有关吗?
我通过阅读 Amit Singh 对他的“hfsdebug”实用程序的解释,发现了有关超级神奇、超级隐藏位置的信息。 如果您想了解更多详细信息,请参阅他的网站 Amit Singh 的 hfsdebug 实用程序。 这是一个非常有趣的软件,会告诉您许多有关 HFS+ 文件系统的详细信息。 它是免费的,我鼓励您下载并尝试一下。 它不再受支持,但仍然适用于 Snow Leopard 和 Leopard - 基本上任何 HFS+ 支持的系统。 你不能用它真正造成任何伤害,因为它是一个“只读”工具 - 所以它非常适合用来查看文件系统的一些细节。
关于这些“文件夹的硬链接”的另一个问题 - 一旦您创建了一个并且超级神奇的超级秘密隐藏文件夹被创建,它就永远存在。 即使您取消链接最初创建它的文件夹,这个神奇的文件夹仍然存在。 不知道为什么,但确实如此。 如果您想尝试一下,可以使用“hfsdebug”来找出答案。 您还可以使用“hfsdebug”来查明驱动器上存在多少个“文件夹硬链接”。 有关这些详细信息,请参阅 Amit 关于“hfsdebug”实用程序的文章。
他还有另一个较新的实用程序,该实用程序受到支持,但需要付费。 它称为 fileXray,一个人在同一家庭中任意数量的计算机上购买个人非商业类型许可证的费用为 79 美元。 它有一份长达 173 页的详细用户指南,您可以在购买之前下载该指南以了解它的功能。 不幸的是,没有试用版,因此请阅读手册并查看网站以获取更多详细信息,看看它是否可以帮助您摆脱困境。 在其网站上了解有关它的所有详细信息 - 请参阅 fileXray 网站 了解更多信息。
使用这些文件夹硬链接时,您应该注意几个问题。 如果创建它们的卷安装到远程客户端,则可能会出现严重问题,具体取决于它们的安装方式。 如果您使用 AFP 将卷安装到远程客户端,则会出现大问题,因为任何当前具有硬链接或曾经有一个但后来被删除的文件夹将无法用作所有较低级别的文件夹(但文件除外)将无法从 Finder 或终端窗口访问。 如果您尝试执行简单的“ls -lR”命令,它将失败并为所有较低级别的文件夹提供“ls:xxx:没有这样的文件或目录”错误消息。 如果您使用 Finder 窗口遍历远程卷的目录树,则当您第一次单击文件夹名称时,具有硬链接的文件夹中的文件夹将消失,不会出现任何错误。
如果您使用 NFS 挂载远程客户端(并假设系统上有一个 NFS 服务器,且该卷具有本地 HFS+ 文件系统),则似乎不会出现这些问题(错误消息除外)。 这里不详细介绍如何使用 NFS 挂载卷。 我使用了 Marcel Bresink 博士提供的一个名为“NFS Manager”的优秀程序来帮助在服务器和客户端上安装 NFS。 您可以从他的网站上获取它 - 只需在您最喜欢的搜索引擎中搜索“Bresink NFS Manager”,但他有免费试用版,因此您可以在购买前试用。 如果您想学习如何进行 NFS 安装,这并不是什么大问题,但“NFS 管理器”使设置和调整所有不同设置以帮助优化它变得非常容易。 他还有其他几个简洁的 Mac OS X 实用程序,价格非常合理 - 一个称为“硬件监视器”,可让您监视和绘制各种内容,例如电源使用情况、CPU 温度、风扇速度以及许多其他变量。长时间(从几分钟到几天)本地和远程 Mac 系统。 如果您喜欢方便的实用程序,绝对值得一试。
我注意到的一件事是 NFS 文件传输比通过 AFP 慢大约 20%,但你的“里程可能会有所不同”,所以不能保证一种方式或另一种方式,但我宁愿拥有一些可以工作的东西,即使我有与什么都不做相比,付出 20% 的性能损失。
Apple 意识到硬链接和远程 AFP 文件系统的问题,他们将其称为 AFP 客户端的“实现限制” - 我更喜欢称其为我真正认为的那样 - 一个 BUG! 我只能希望 Mac OS X 的下一个版本能够解决这个问题,因为我真的很喜欢能够在有意义的情况下使用文件夹的硬链接。
这些注释是我个人的观点,我不对其正确性做出任何保证,因此使用它们的风险由您自行承担。 在使用这些“文件夹硬链接”之前,请做好备份,以防发生意外情况。 但如果您决定更多地了解 Mac OS X 的这个有趣的方面,我希望您能玩得开心。
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.
Snow Leopard can create hard links to directories as long as you follow Amit Singh's six rules:
So it's not correct at all that Snow Leopard has lost the ability to create hard links to
folders.
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.
Here is the "hunlink.c" program:
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.
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
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.
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.
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.
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?
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.
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.
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 site for more info.
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.
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.
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.
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.
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.
那么你不能直接在 BASH 中执行此操作。 但是...我在这里找到一篇文章讨论如何间接做到这一点: http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html 通过编译一个简单的小 C 程序:
...并在 Terminal.app 中构建和:
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:
...and build in Terminal.app with:
废话。 在 10.5 上,它在 ln 的手册页中告诉您:
所以是的:
给它您的密码,然后您还没有完成。 你没有记录下来,是吗? 您必须记录硬链接目录; 即使它是单用户计算机。
删除是一个不同的故事:如果您按照通常的方式删除目录,您将删除其中的内容。 因此,您必须“取消链接”该目录:
那里。 希望你不要破坏你的文件系统!
Piffle. On 10.5, it tells you in the man page for ln:
So yes:
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:
There. Hope you don't wreck your filesystem!
交叉发布这个很棒的工具可以巧妙地解决问题,最初由Sam:
要安装硬链接,请确保您已安装 homebrew,然后运行:
安装后,创建硬链接:
我还注意到
unlink
命令在雪豹上不起作用,所以我添加了一个取消链接的选项:代码可用感兴趣的人可以在 Github 上查看:https://github.com/selkhateeb/hardlink
Cross-posting this great tool which neatly solves the problem, originally posted by Sam:
To install Hardlink, ensure you've installed homebrew, then run:
Once installed, create a hard link with:
I also noticed that
unlink
command does not work on snow leopard, so I added an option to unlink:Code is available on Github for those who are interested: https://github.com/selkhateeb/hardlink
是的,它受到内核和文件系统的支持,但由于它不适合一般用途,因此不会暴露给 shell。
您可能可以找出 Time Machine 使用哪些 API 并将它们包装在命令行工具中,但最好接受提示并清楚地引导。
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.
OSX 版本的 ln 无法做到这一点,但是,正如 rich 的另一个答案中提到的,可以使用
ln
的 GNU 版本,它可以在 homebrew 中作为获得gln
作为 coreutils 公式的一部分。man gln
列出了-d
选项以及 rich 中提供的特定于 OSX 的警告 的回答。 换句话说,它并非在所有情况下都有效。 到底是什么决定了它是否有效似乎没有任何记录。作为先决条件,安装
coreutils
:现在您可以执行以下操作:
重要:要删除您必须使用的硬链接
枪链接
:仅供参考:coreutils自制程序提供了通用unix工具的GNU兼容版本。 使用
brew list coreutils
查看完整列表。The OSX version of
ln
cannot do it, but, as mentioned in the other answer by rich, it is possible with the GNU version ofln
which is available in homebrew asgln
as part of the coreutils formula.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.As a prerequisite, install
coreutils
:Now you can do:
IMPORTANT: To remove the hard link you must use
gunlink
:FYI: The coreutils homebrew formula provides the GNU-compatible versions of generic unix tools. Use
brew list coreutils
to see the full list.从2018年开始就不可能了。 APFS(在 MacOS High Sierra 10.13 中引入)与目录硬链接不兼容。 请参阅https://github.com/selkhateeb/hardlink/issues/31
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
我的情况是,我发现在 Windows 虚拟机中,我无法跟踪符号链接。 (我想在 Internet Explorer 中测试一些 HTML 页面)。 我的目录结构有 CSS 和图像文件夹的符号链接。
我解决问题的方法是采用与其他答案所暗示的不同的方法。 我使用 rsync 创建该文件夹的副本。 Rsync 可以解析符号链接并复制链接的文件。
这解决了我的问题,而无需使用目录的硬链接。 如果您只处理一小部分文件,这实际上是一个简单的解决方案。
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.
从链接到的文章中,如果您尝试在与原始目录相同的目录中创建硬链接,您将收到该错误。 您必须在其他地方创建它。
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.
另一种解决方案是使用bindfs https://code.google.com/p/bindfs/可以通过端口安装:
Another solution is to use bindfs https://code.google.com/p/bindfs/ which is installable via port:
在Linux中,您可以使用bind mount来模拟硬链接目录。 不确定 OSX
In Linux you can use bind mount to simulate hard linking directories. Not sure about OSX
这也可以使用内置 Perl(从终端)完成,无需编译任何内容。 我的具体用例是 Google Drive(不支持符号链接),因此下面的示例反映了该用例。
要将“文档”文件夹链接到 Google 云端硬盘以使其同步:
要从 Google 云端硬盘中删除指向“文档”文件夹的链接:
您需要“root”才能取消链接(请参阅“取消链接”perldoc)。
This can also be done with built-in Perl (from Terminal) without compiling anything. My specific use case is for Google Drive (which doesn't support symbolic links), so the examples below reflect the use case.
To link your "Documents" folder to Google Drive so it's synced:
To remove the link to your "Documents" folder from Google Drive:
You need "root" to unlink (see "unlink" perldoc).
简短的回答是你不能。 :)(除了可能作为 root 的情况,更准确地说你不应该这样做。)
Unix 只允许一定数量的目录链接 - 来自其所有子目录的“..”和“.”。 来自其自身内部。 其他任何东西都可能导致非常混乱的目录树。 这显然是肯·汤普森的设计决定。
(话虽如此,显然苹果的时间机器确实做到了这一点:))
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 :) )
如果没有子文件夹,您可以尝试
lnfolder_path/*。* target_folder
它对我有用在 OSX 10.9 上
in case there is no sub folder, you can try
ln folder_path/*.* target_folder
it worked for me on OSX 10.9