<p>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.</p>

[{'answer_id': 81000, 'author': 'Penfold', 'author_id': 11952, 'author_profile': 'https://Stackoverflow.com/users/11952', 'pm_score': -1, 'selected': False, 'text': '<p>The short answer is you can\'t. :) (except possibly as root, when it would be more accurate to say you shouldn\'t.)</p>\n\n<p>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.</p>\n\n<p>(Having said that, apparently Apple\'s Time Machine does do this :) )</p>\n'}, {'answer_id': 81163, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 805001, 'author': 'username', 'author_id': 4939, 'author_profile': 'https://Stackoverflow.com/users/4939', 'pm_score': 6, 'selected': True, 'text': '<p>You can\'t do it directly in BASH then. However... I found an article here that discusses how to do it indirectly: <a href="http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html" rel="noreferrer">http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html</a> by compiling a simple little C program:</p>\n\n<pre><code>#include &lt;unistd.h&gt;\n#include &lt;stdio.h&gt;\n\nint main(int argc, char *argv[])\n{\n   if (argc != 3) return 1;\n\n   int ret = link(argv[1], argv[2]);\n\n   if (ret != 0) perror("link");\n\n   return ret;\n}\n</code></pre>\n\n<p>...and build in Terminal.app with:</p>\n\n<pre><code>$ gcc -o hlink hlink.c -Wall\n</code></pre>\n'}, {'answer_id': 1565096, 'author': 'Jesper Rønn-Jensen', 'author_id': 109305, 'author_profile': 'https://Stackoverflow.com/users/109305', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>My workaround to solve the problem was a different approach than the other answers implied. I used <code>rsync</code> to create a copy of the folder. Rsync can resolve the symlinks and copy the linked files in stead.</p>\n\n<p>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.</p>\n\n<pre><code>rsync -av --copy-dirlinks --delete ../htmlguide ~/src/\n</code></pre>\n"}, {'answer_id': 2038065, 'author': 'Rich', 'author_id': 247592, 'author_profile': 'https://Stackoverflow.com/users/247592', 'pm_score': 4, 'selected': False, 'text': '<p>Piffle. On 10.5, it tells you in the man page for <strong>ln</strong>:</p>\n\n<pre><code>   -d, -F, --directory\n          allow the superuser to attempt to hard link  directories  (note:\n          will  probably  fail  due  to  system restrictions, even for the\n          superuser)\n</code></pre>\n\n<p>So yes:</p>\n\n<pre><code>    sudo  ln  -d  existing_dir  new_hard_link\n</code></pre>\n\n<p>Give it your password, and <strong>you\'re not done yet</strong>. You didn\'t document it, did you? You <strong>must</strong> document hard linked directories; even if it\'s a single user machine.</p>\n\n<p>Deleting is a different story: if you go about it the usual way to delete directories, you\'ll delete the contents. So you <strong>must</strong> "unlink" the directory:</p>\n\n<pre><code>    unlink  new_hard_link\n</code></pre>\n\n<p>There. Hope you don\'t wreck your filesystem!</p>\n'}, {'answer_id': 3938981, 'author': 'Gannet', 'author_id': 802862, 'author_profile': 'https://Stackoverflow.com/users/802862', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 4707231, 'author': 'Bob', 'author_id': 577700, 'author_profile': 'https://Stackoverflow.com/users/577700', 'pm_score': 6, 'selected': False, 'text': '<p>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.</p>\n<p>Snow Leopard can create hard links to directories as long as you follow Amit Singh\'s six rules:</p>\n<ol>\n<li>The file system must be journaled HFS+.</li>\n<li>The parent directories of the source and destination must be different.</li>\n<li>The source’s parent must not be the root directory.</li>\n<li>The destination must not be in the root directory.</li>\n<li>The destination must not be a descendent of the source.</li>\n<li>The destination must not have any ancestor that’s a directory hard link.</li>\n</ol>\n<p>So it\'s not correct at all that Snow Leopard has lost the ability to create hard links to\nfolders.</p>\n<p>I just verified that link/unlink do work on Snow Leopard - as long as you follow the six\nrules. 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.</p>\n<p>Here is the &quot;hunlink.c&quot; program:</p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\nint\nmain(int argc, char *argv[])\n{\n   if (argc != 2)\n      return 1;\n   int ret = unlink(argv[1]);\n   if (ret != 0)\n      perror(&quot;unlink&quot;);\n   return ret;\n}\n\ngcc -o hunlink hunlink.c\n</code></pre>\n<p>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\nwhat you\'ve done for later on or for someone else who might need to know this.</p>\n<p>One other &quot;gotcha&quot; that I just learned about these &quot;hard links&quot; to folders. When you create them there is really a lot that happens &quot;behind the curtain&quot; 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 &quot;source_folder&quot; - remember the format of the command is</p>\n<pre><code>hlink source_folder target_folder\n</code></pre>\n<p>So because of this, you have to be careful of not having any files open in the &quot;source_folder&quot; 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 &quot;source_folder&quot;. 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 &quot;ls -la&quot; command any longer without getting funny errors for all the folders/directories that were in the original &quot;source_folder&quot; but you could do a &quot;ls&quot; command and all looked well.</p>\n<p>If you run &quot;Verify disk&quot; in the &quot;Disk Utility&quot; program, you will notice that it probably complains and gives a &quot;Volume bitmap needs minor repair for orphaned blocks&quot; which is what just happened with the creation of the super-magical folder and the movement of the &quot;source_folder&quot; to it.</p>\n<p>If you do find yourself in this situation with &quot;orphaned blocks&quot;, first save the changed files to some other temporary location not in the volume containing the &quot;source_folder&quot; tree, then use &quot;Disk Utility&quot; to unmount and remount the volume that contains the &quot;source_folder&quot; 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.</p>\n<p>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 &quot;journaled&quot; file system?</p>\n<p>I discovered the info about the super-magical, super-hidden location by reading Amit Singh\'s explanation of his &quot;hfsdebug&quot; utility. If you want more details see his web site at <a href="https://web.archive.org/web/20200910033812/https://osxbook.com/blog/2008/11/09/hfsdebug-40-and-new-hfs-features/" rel="nofollow noreferrer">Amit Singh\'s hfsdebug utility</a>. 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 &quot;read-only&quot; tool - so it\'s great to use to look at some details of the filesystem.</p>\n<p>One more issue about these &quot;hard links to folders&quot; - 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 &quot;hfsdebug&quot; to find this out if you wish to try it out. You can also use &quot;hfsdebug&quot; to find out how many of these &quot;hard links to folders&quot; exist on a drive. For these details refer to Amit\'s article on the &quot;hfsdebug&quot; utility.</p>\n<p>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 <a href="https://web.archive.org/web/20200103162305/https://filexray.com/" rel="nofollow noreferrer">fileXray web site</a> for more info.</p>\n<p>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 &quot;ls -lR&quot; command, it will fail and give you &quot;ls: xxx: No such file or directory&quot; 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.</p>\n<p>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 &quot;NFS Manager&quot; to help with the NFS mounts on the server and client. You can get it from his web site - just search for &quot;Bresink NFS Manager&quot; 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 &quot;NFS Manager&quot; 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 &quot;Hardware Monitor&quot; 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.</p>\n<p>One thing I did notice is that NFS file transfers were about 20% slower than doing them via AFP, but your &quot;mileage may vary&quot;, 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.</p>\n<p>Apple is aware of the problems with hard links and remote AFP filesystems, and they refer to it as an &quot;implentation limitation&quot; 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.</p>\n<p>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 &quot;hard links to folders&quot; 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.</p>\n'}, {'answer_id': 14842414, 'author': 'Kit Sunde', 'author_id': 29347, 'author_profile': 'https://Stackoverflow.com/users/29347', 'pm_score': 0, 'selected': False, 'text': '<p>Another solution is to use bindfs <a href="https://code.google.com/p/bindfs/" rel="nofollow">https://code.google.com/p/bindfs/</a> which is installable via port:</p>\n\n<pre><code>sudo port install bindfs\nsudo bindfs ~/source_dir ~/target_dir\n</code></pre>\n'}, {'answer_id': 20601652, 'author': 'Nuray Altin', 'author_id': 577674, 'author_profile': 'https://Stackoverflow.com/users/577674', 'pm_score': -1, 'selected': False, 'text': '<p>in case there is no sub folder, you can try</p>\n\n<p><strong>ln folder_path/*<em></strong>.</em><strong>* target_folder</strong></p>\n\n<p>it worked for me on OSX 10.9</p>\n'}, {'answer_id': 36540025, 'author': 'zainengineer', 'author_id': 3232611, 'author_profile': 'https://Stackoverflow.com/users/3232611', 'pm_score': 1, 'selected': False, 'text': '<p>In Linux you can use bind mount to simulate hard linking directories. Not sure about OSX</p>\n\n<pre><code>sudo mount --bind /some/existing_real_contents /else/dummy_but_existing_directory\nsudo umount /else/dummy_but_existing_directory\n</code></pre>\n'}, {'answer_id': 37742750, 'author': 'Simon East', 'author_id': 195835, 'author_profile': 'https://Stackoverflow.com/users/195835', 'pm_score': 4, 'selected': False, 'text': '<p><em>Cross-posting <a href="https://stackoverflow.com/a/5118678/195835">this great tool</a> which neatly solves the problem, originally posted by <a href="https://stackoverflow.com/users/634331/sam">Sam</a>:</em></p>\n\n<hr>\n\n<p>To install Hardlink, ensure you\'ve installed <a href="http://brew.sh/" rel="noreferrer">homebrew</a>, then run:</p>\n\n<pre><code>brew install hardlink-osx\n</code></pre>\n\n<p>Once installed, create a hard link with:</p>\n\n<pre><code>hln [source] [destination]\n</code></pre>\n\n<p>I also noticed that <code>unlink</code> command does not work on snow leopard, so I added an option to unlink:</p>\n\n<pre><code>hln -u destination\n</code></pre>\n\n<hr>\n\n<p>Code is available on Github for those who are interested: <a href="https://github.com/selkhateeb/hardlink" rel="noreferrer">https://github.com/selkhateeb/hardlink</a></p>\n'}, {'answer_id': 39472511, 'author': 'techiejohn', 'author_id': 4805400, 'author_profile': 'https://Stackoverflow.com/users/4805400', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>To link your "Documents" folder to Google Drive so it\'s synced:</p>\n\n<pre><code>perl -e \'link "/Users/me/Documents", "/Users/me/Google Drive/Documents"\'\n</code></pre>\n\n<p>To remove the link to your "Documents" folder from Google Drive:</p>\n\n<pre><code>sudo perl -U -e \'unlink "/Users/me/Google Drive/Documents"\'\n</code></pre>\n\n<p>You need "root" to unlink (see "unlink" perldoc).</p>\n'}, {'answer_id': 39891753, 'author': 'ccpizza', 'author_id': 191246, 'author_profile': 'https://Stackoverflow.com/users/191246', 'pm_score': 3, 'selected': False, 'text': '<p>The OSX version of <code>ln</code> cannot do it, but, as mentioned in the other answer by <a href="https://stackoverflow.com/a/2038065/191246">rich</a>, it is possible with the GNU version of <code>ln</code> which is available in <a href="http://brew.sh" rel="nofollow noreferrer">homebrew</a> as <code>gln</code> as part of the <a href="http://brewformulas.org/Coreutil" rel="nofollow noreferrer">coreutils</a> formula. <code>man gln</code> lists the <code>-d</code> option with the OSX-specific warning provided in <a href="https://stackoverflow.com/a/2038065/191246">rich</a>\'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.</p>\n\n<p>As a prerequisite, install <code>coreutils</code>:</p>\n\n<pre class="lang-bash prettyprint-override"><code>    brew install coreutils\n</code></pre>\n\n<p>Now you can do:</p>\n\n<pre class="lang-bash prettyprint-override"><code>    sudo gln -d /original_folder /mirror_folder\n</code></pre>\n\n<p><strong>IMPORTANT</strong>: To remove the hard link you <strong><em>must</em></strong> use <code>gunlink</code>:</p>\n\n<pre><code>    sudo gunlink /mirror_folder\n</code></pre>\n\n<blockquote>\n  <p>❗️❗️❗️ <strong>Using <code>rm</code> or Finder will also delete the original folder.</strong></p>\n</blockquote>\n\n<p>FYI: The <a href="http://brewformulas.org/Coreutil" rel="nofollow noreferrer">coreutils</a> homebrew formula provides the GNU-compatible versions of generic unix tools. Use <code>brew list coreutils</code> to see the full list.</p>\n'}, {'answer_id': 52754343, 'author': 'ariel', 'author_id': 242076, 'author_profile': 'https://Stackoverflow.com/users/242076', 'pm_score': 2, 'selected': False, 'text': '<p>As of 2018 no longer possible. APFS (introduced in MacOS High Sierra 10.13) is not compatible with directory hardlinks. See <a href="https://github.com/selkhateeb/hardlink/issues/31" rel="nofollow noreferrer">https://github.com/selkhateeb/hardlink/issues/31</a></p>\n'}]
['https://Stackoverflow.com/questions/80875', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4939/']