OS X has been in the public’s hands, and on their Macs, for sixteen years now – the Public Beta of OS X 10.0 was released on 13 September 2000 – but if there’s one central topic which almost no one really understands, it must be aliases and links.
Most of us just settle for what the Finder offers: a quick Command-L to make a regular alias, and we’re happy. Some know that there are also Unix-style links available, but for the majority of those power users, the differences between them are unclear. The end result is that only real wizards dabble with links, although there are times when they are much better than aliases. Take a look at your Time Machine backup: that uses hard links throughout, and simply couldn’t work if it relied on aliases instead.
Forget, for the moment, Finder aliases, and consider the traditional links offered by Unix. These come in two flavours: the hard link, and the symbolic.
A hard link is a place marker to the actual file in the storage. Any reference to that place marker is automatically taken to the original file, as if it were the original itself. Ask for the size, permissions, or contents of the hard link and you will be given those for the original file itself. You cannot edit a hard link to point to a different file, and you cannot create a hard link to a folder: they only exist for files.
Because they are so efficient, hard links are ideal in Time Machine backups. Look in your latest backup and you will see that it appears to contain every file on your disk, although the great majority were not backed up recently. This is because most of what appear to be files are in fact hard links to the most recent original, which may actually be many backups ago. Every backup since then has just created another hard link which refers back to that original. Your backup volume may well include several million hard links: because they occupy almost no space at all and are so efficient, this is not a problem.
A symbolic link (symlink, or soft link) is quite different, as it is a tiny file which contains the path to the original file, making it a symbol for that file. Thus a symbolic link will only work so long as the original file remains in the same place, and keeps the same name. Change the name of the original, or move it to a different folder, and all symbolic links to that file will break.
Symbolic links can point to folders as well as files, and are effectively a “poor person’s alias”. For OS X users, they have limited application, as we tend to do terrible things like move folders around, only to discover that all the symbolic links inside have been broken. Their one potential benefit to a Mac user is that – so long as the file name and its path remain unchanged – the actual file or folder can change completely. Because a symbolic link consists of just a path and filename, it is also very economical in terms of storage space.
These first appeared way back in 1991, in System 7 for classic Macs, so have had plenty of time to mature. They are also the only type of link which is fully usable from the Finder and graphical interface; Unix links have to be created and maintained in Terminal’s command line.
Finder aliases have most of the best features of both types of Unix link, so are, in almost all respects, the best deal possible. When you access a file or folder through an alias, OS X first looks in the path stored, for the original file name, as if it was a symbolic link. If OS X cannot find the path or file/folder as expected, it then falls back to behave like a hard link: it looks for the original file via a place marker to its original in storage.
This makes aliases tolerant of moving the original file or folder, which is why they are so popular. The only snags with this are that their versatility comes at the cost of requiring more storage for the alias itself, and, being more complex, you would not wish to have millions of them on any given volume.
Appearance and use
In the Finder view above, you can see the normal icons shown for a text file and a folder in different forms. From the left, these are the original, a Finder alias, a hard link (which can only be to a file, not a folder), and a symbolic link. This is slightly confusing, as the Finder does not distinguish visibly between aliases and symbolic links. If you do use symbolic links, you may wish to make that distinction clear, because of the different effects of moving the original on the alias/link.
Creating a Finder alias to a file or folder is easy: select the original, and press Command-L, use the popup contextual menu, or the command in the Finder’s File menu.
When you use Get Info on the item, it is shown to be an Alias, with a relatively large size which is independent of the size of the original. There is also a button to select a new original, should you wish to change the link. The path to the original is given, should you want to check it.
Information given in Terminal’s command line, in response to
ls -la, gives no indication that an alias is any form of link:
-rw-r--r--@ 1 hoakley staff 1268812 31 Aug 06:36 folder1 alias
-rw-r--r--@ 1 hoakley staff 443544 31 Aug 06:35 notes.txt alias
If you perform any actions on aliases in the command line, you will discover that they behave as if they were normal files, not links.
A hard link can only be made to a file, not a folder, and must be made in Terminal’s command line using a command like
ln notes.txt noteshard.txt
which creates a hard link named noteshard.txt to the original notes.txt in the current folder. If you list the link using the command
ls -la, it is shown as being identical to the original file, as OS X sees straight to that original via the link:
-rw-r--r--@ 2 hoakley staff 7298 28 Aug 18:10 noteshard.txt
The same happens in the Finder, which does not show the link as a separate object, but gives all the details for the original file.
One snag with hard links is that all hard links to a file must be deleted before that file can be deleted. This is one good reason why you should never tamper with Time Machine backups yourself, but allow Time Machine to handle them: if you try trashing any given backup, you could be trying to trash the original files for tens of thousands of hard links, which need to be removed first. Time Machine should make a much better job than you can.
A symbolic link can be made to a file or folder, but must be created in Terminal’s command line using a command like
ln -s folder1 folder1symb
where the -s option creates a symbolic rather than hard link, folder1 is the original, and folder1symb is the link. If you list the link using the command
ls -la, the link is shown as being a tiny file, and the link destination is indicated:
lrwxr-xr-x 1 hoakley staff 7 31 Aug 06:43 folder1symb -> folder1
lrwxr-xr-x 1 hoakley staff 9 31 Aug 06:44 notessymb.txt -> notes.txt
In the Finder, Get Info shows that the link as an Alias (which is misleading, of course), and gives the path and name of the original to which it links. This all looks very similar to a Finder alias, except that its size is tiny, and there is no button to select a new original, as you cannot edit symbolic links in the Finder.
There are very few situations in which you are likely to want to use a symbolic link, but you may well come across them. Hard links are valuable, and every time that you access your Time Machine backups you are using them. If you support or maintain Mac systems, they are an important tool which one day may save your skin, and understanding them is vital. But for the great majority of everyday tasks on your Mac, the Finder alias is ideal.