Hack 9 Aliases, Symlinks, and Hard Links


Poking about with aliases, symlinks, and hard links reveals some interesting entanglements in the merging of the Mac GUI and its Unix underpinnings.

Aliases (shortcuts, if you're from the Windows world) are indispensable for those of us who insist upon filing things in more than one place or like to have access to particular groupings of applications, files, and whatnot within easy reach. An alias provides a trail of bread crumbs to the original item aliased, keeping track of it no matter where it might reside. It was common in OS 9 to add aliases for your oft-used applications and folders either right on the desktop or in the Apple menu, or, indeed, both. Now, thanks to the Dock and some Dock alternatives [Hack #51], there's little need to clutter your Apple menu or Desktop with aliases.

That's not to say that OS X doesn't have aliases; it does indeed. Simply select a file, folder, application, or whatnot and select File Make Alias or press figs/command.gif-L (in OS 9 it was figs/command.gif-M, which now, sadly, minimizes the current window instead).

OS X being a hybrid of the Mac and Unix worlds causes some interesting entanglements when it comes to keeping track of the locations of things and their aliases. Mac OS X does a seamless job of glossing over the details. That doesn't mean, however, it's not worth poking about a bit.

The Unix world's aliases ? actually called links ? come in two flavors: hard and soft (symbolic). With a hard link, two or more filenames point to the same data on disk; think my house, our house, and the house where I live. A symbolic link (a.k.a. soft link or symlink) is a different file from the original, holding nothing but a link to the original's filename; think address book, signpost, or bank account number. Remove one of two hard links and your data still exists. Remove the last remaining hard link and a symlink doesn't do you a bit of good, holding no real data itself.

The ln command creates and alters links on the command line via the Terminal [Hack #48]. Figure 1-20 shows me creating a file, original, then hard linking, and symlinking to it. Note that the original alias was created on the desktop via the Finder's Make Alias option.

Figure 1-20. Creating an alias on the command line

As the Info box shows, OS X sees no difference between a symlink created on the command line and an alias created via the Make Alias command. However, from the command-line point of view, this is not the case; the original alias is an entirely different file. Edit it and you'll find you've not touched the original, nor does it have any effect on the GUI view of the original alias itself at all. Mac OS X appears to care only about knowing it's an alias from the GUI side, leaving the Unix side to treat it as a regular file.

Touching the original (touch original) again to alter its timestamp affects only the original and hard link as expected, since they are indeed the same file.