OS X hardlink

UPDATE 2013.02.09: Via the comments I’ve come to know that it is possible with ln after all as long as you don’t combine a tilde (˜) and quotes in the source path (which I did because some of my folders have spaces in their name). Just escape any spaces with a \ and the tilde will resolve fine. No need for one to install hardlink. Thanks @mathias!

A simple command-line utility that implements directory hardlinks on Mac OS X

Because OS X’ built-in ln is crippled. Install via:

git clone git://github.com/selkhateeb/hardlink.git
make
sudo make install

Usage

# create hardlink
hardlink source destination

#remove hardlink
hardlink -u destination

Thanks to this one I can now cd into ~/Kaho/ws2 instead of ~/Dropbox/Kaho/Lesactiviteiten/WS2 - Serverside/2012-2013 🙂

hardlink →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Join the Conversation

11 Comments

  1. In the situation you described, couldn’t you have done the same with a simple symlink? That’s what I do for my dotfiles directory. It lives in ~/Projects/dotfiles, but I have it symlinked as ~/dotfiles so I can simply cd dotfiles from my home directory to get there.

    $ ls -lsa | grep dotfiles
    8 lrwxr-xr-x 1 Mathias staff 32 Jul 29 2012 dotfiles -> /Users/Mathias/Projects/dotfiles

      1. Symlinks are fine and dandy but turns out one can’t cd into a symlink on the shell (at least I couldn’t).

        Another reason I create a hardlink is to shorten the path displayed in Terminal (cfr. your .bash_prompt). That way I can keep the username+path+branch on a single line, even with large font sizes (which I use when my MBP is connected to a projector so that even my students in the back can see what happens).

          1. bramus in ~/Kaho
            $ ln -s '~/Dropbox/Kaho/Lesactiviteiten/WS2 - Serverside/2012-2013/' test
            
            bramus in ~/Kaho
            $ ls
            total 8
            drwxr-xr-x  10 bramus  staff   340 Feb  8 16:37 .
            drwxr-xr-x+ 54 bramus  staff  1836 Feb  8 16:10 ..
            lrwxr-xr-x   1 bramus  staff    58 Feb  8 16:37 test -> ~/Dropbox/Kaho/Lesactiviteiten/WS2 - Serverside/2012-2013/
            ...
            
            bramus in ~/Kaho
            $ cd test
            -bash: cd: test: No such file or directory

            🙁

    1. … and once again Mathias turned out to be the hero that saved the day. Thank you, dear Sir!

      Also: Why Apple, Why? That’s stuff that should work out of the box.

      1. > Also: Why Apple, Why? That’s stuff that should work out of the box.

        this isn’t an apple thing. this is a *nix thing. the tilde is a shell expansion – if you single quote it, no expansion happens. Think of ‘~’ just like you would ‘$HOME’ or ‘*fish’ – they’re all literal strings.

        on the other hand, double quotes ARE expanded. “~” “$HOME” “*fish” (the later is a shell glob, if you’re not familiar with that – and would expand if it matches something, otherwise is literal)

        fwiw.

  2. I tried your ‘hardlink’ utility on OSX 10.6.8. The link option worked fine. But now I can’t unlink.

    At first I tried to just do another ‘hardlink’ command to the same destination, but it complained that ‘File exists’. Then I tried to ‘rmdir’ the new link: ‘Operation not permitted’. I’ve decided that I can use a symlink for the effect I want, but I just can’t get rid of this experiment.

    Any thoughts?

  3. I just wanted to leave a note thanking you for making this! Super useful. I use hardlinks on linux and windows all the time and was struggling with ‘ln’ on OSX before I found out that ln is crippled.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.