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 →

Elsewhere , ,

10 Responses to OS X hardlink

  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

    • (Or just create an alias: alias ws2="cd ~/Dropbox/Kaho/Lesactiviteiten/WS2\ -\ Serverside/2012-2013".)

      • Bramus! says:

        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).

        • The symlink solution I described fulfills all your requirements :)

          Strange it’s not working for you…

          • Bramus! says:
            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

            :(

  2. Tilde expansion won’t work in strings that are wrapped in single quotes. Try this instead:

    ln -s ~/Dropbox/Kaho/Lesactiviteiten/WS2\ -\ Serverside/2012-2013 test

    • Jan! says:

      You can also do ln -s ~/'Dropbox/Kaho/Lesactiviteiten/WS2 - Serverside/2012-2013/' test. Like Mathias said, just don’t quote or escape the tilde.

    • Bramus! says:

      … 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.

  3. Jerry Barrington says:

    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?

  4. Tarun Elankath says:

    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 Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>