Thursday, February 19, 2009

Symbolic links in Unix

I got an idea today from a Lifehacker post from a few days ago. The article is about how to create a symbolic link to a file or folder for use with Dropbox to get it to sync multiple folders. A symbolic link is a file that contains a reference to another file or folder. The idea of that Lifehacker post was to reference another folder and store the symbolic link in the Dropbox folder so the contents of the linked folder get synced as well. That's a really neat idea, but I did mine for a different reason.

Lately I've been doing a good bit of command line work with Java files on my Mac laptop. I keep these files stored in my deep, yet organized, directory structure. New Terminal sessions start in the Home folder. Rather than having to navigate to the directory where I'm storing my files, I created a symbolic link for that directory in my home directory. The link works through both Terminal and Finder.

Here's how it works:
type "ln -s [path to linked directory] [location of link]"

So if you have a directory located at "~/Documents/Recent/Code/Java/SweetProject" and you want a symbolic link to in your Home directory you would type "ln -s ~/Documents/Recent/Code/Java/SweetProject ~/". This would create a symbolic link called SweetProject in your Home directory. To navigate to that directory all you have to type now is "cd SweetProject". The link is visible in Finder and is renamable if you want to call it something different.

By default, the ln command creates a hard link to a file which appears in Finder as being an exact copy of that file but is actually only a link to it. The -s switch is what makes this a symbolic link which allows the link to refer to directories.

More on symbolic links.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.