Tuesday, December 31, 2013

Hard Link Directories in OSX

Ever had one of those days when things that worked fine just don't anymore?

Today, for some reason I have yet to discover, my soft linked directories (created with ln -s <SOURCE> <TARGET>) became unsearchable by the bash find command.

I had to resort to creating a C program, in order to use it's link function.

SOURCE


#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
Purpose:  To create a hard link to a directory
Install:  gcc hln.c -o hln
*/
int main(int argc, char* argv[]) {
    //Make sure we have the right arguments
    if (argc != 3) {
        fprintf(stderr,"Usage:   hln <SOURCE_DIRECTORY> <TARGET_DIRECTORY>\n\n");
        fprintf(stderr,"To later unlink target directory:   hln -u <TARGET_DIRECTORY>\n");
        return 1;
    }
    int ret = 0;
    if(strcmp(argv[1], "-u") == 0)
        ret = unlink(argv[2]);
    else
        ret = link(argv[1],argv[2]);
         
    if (ret != 0)
        perror("hardlink");
    return ret;
}

Compile it

After compiling, you'll want to copy the hln executable to a suitable directory, like /usr/bin.

$ gcc hln.c -o hln

All good

Now, I can create (hard) linked directories and unlink them (with the -u option).

2 comments: