Mac OS X: Symbolic Links

If you’ve ever made an Alias to a file in classic Mac OS, or a Shortcut to a file in Windows, you will easily be able to understand the UNIX equivalent (where aliases and shortcuts came from in the first place), called a Symbolic Link. The easiest definition to understand is directly from the man page, “[A Link] is useful for maintaining multiple copies of a file in many places at once without using up storage for the ‘copies’; instead, a link ‘points’ to the original copy.”

Aliases

In Mac OS System 7 and later, an alias is a small file that represents another object in a local, remote, or removable file system and provides a dynamic link to it; the target object may be moved, and the alias will still link to it (unless the original file is recreated; such an alias is ambiguous and how it is resolved depends on the version of Mac OS X). Examples of the information used to locate the original are: path, file ID, directory ID, name, file size.

In Windows, the same function is performed with a “shortcut” (a file with a .lnk extension); in most circumstances, moving the target file breaks the link.

Alias is similar to the Unix symbolic link, but with the added benefit of working even if the target file moves to another location on the same disk (in this case it acts like hard link, but the source and target of the link may be on different filesystems). As a descendant of BSD, Mac OS X supports Unix symbolic (and hard) links as well.

An alias acts as a stand-in for any object in the file system, such as a document, an application, a folder, a hard disk, a network share or removable medium or a printer. When double-clicked, the computer will act the same way as if the original file had been double-clicked. Likewise, choosing an alias file from within a ‘File Open’ dialog box would open the original file. The purpose of an alias is to assist the user in managing large numbers of files by providing alternative ways to access them without having to copy the files themselves. While a typical alias under the classic Mac OS was small, between 1 and 5 KB, under Mac OS X it can be fairly large, more than 500 KB for the alias to a folder.

There are several options for creating an alias:

  • Drag and drop: Click the item you wish to alias and hold down the mouse button. Then, while holding down the Command and Option keys, drag the item to where you want the alias to appear. Instead of moving the original item, this will create an alias at the new location.
  • Contextual menus: Hold down the Ctrl key and click the item you wish to alias. From the contextual menu, select Make Alias. The new alias will appear next to the original.
  • Keystroke: Select the item you wish to alias, and press Command-l (the lowercase L). The new alias will appear next to its original.
  • File menu: Select the item, and then from the File menu, select Make Alias. A new alias icon will appear next to the original.

Once you’ve created an alias, you can move it to a more convenient location, and then rename it or give it a new icon as you wish.

Symbolic Links – Symlinks

A symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data General’s RDOS. Today they are supported by the POSIX operating-system standard, most Unix-like operating systems such as FreeBSD, GNU/Linux, and Mac OS X.

Symbolic links operate transparently for most operations: programs which read or write to files named by a symbolic link will behave as if operating directly on the target file. A symbolic link contains a text string that is automatically interpreted and followed by the operating system as a path to another file or directory. This other file or directory is called the “target”. The symbolic link is a second file that exists independently of its target. If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file. Symbolic links pointing to moved or non-existing targets are sometimes called broken, orphaned, dead or dangling.

A note on Hard Links

Hard links do not normally point to directories, and they cannot link paths on different volumes or file systems.

Hard links act like “twins” in sync, you can’t even tell which is the link and which is the original actually (Get Info on both files will show exactly same type of document, same size, etc). For example you change permissions on one file – and it automatically changes it on the other one, you can’t even delete the file completely without deleting the other. On OS X Tiger it’s not possible to make a hard link to a directory, only to a file (this is no longer true on Leopard, Time Machine uses hard links to folders), also your hard link should be located on the same partition of your drive as the original.

Hard links are not always well supported by high level OS X apps, probably because in a certain way they are even lower level than symbolic links. Hard links are direct “pointers” to a unique identifier – a file system object called “inode” – which describes where all the pieces of a file are as well as various other attributes such as last access, modified and create times. When the file is written to the disk, blocks of data containing the file are given this unique identifier (inode) so that your filesystem knows that “file #000234” is the data starting at disk block #00FF and ending at ##0145.

Hard links have been around since the earliest days of Unix, symbolic links were invented by BSD engineers somewhat later and aliases come from OS Classic.

The Difference

An important difference between an alias and a symlink is that if the original file is moved or renamed and a file with the same name is put at the former place, an alias will still point to the previous version of the file, while a symbolic link will point to the new file at the same place. This behaviour is useful e.g. when you need a reference to a file or a folder which is replaced in regular intervals by new versions, while the previous versions are archived. However a disadvantage of a symlink is that when it doesn’t resolve it just goes broken. It happens because symlinks are just files containing a string with the path to the file, whereas aliases store their information in resource fork thus containing more information about the original.

Although symbolic links are a core feature of Mac OS X, Finder is unable to create them. Also there is no easy way to distinguish an alias from a symbolic link in Finder. Both aliases and symbolic links appear in Finder with a little arrow in their icons. If you use “Get Info” in Finder on a symbolic link, it will show its kind as “Alias” – which is a bit misleading at least. Path Finder knows how to distinguish between symlinks and aliases, displaying their kind correctly and can also create them sparing you a trip to terminal.

On HFS and HFS+ file systems, each file and folder has a unique, persistent identity. Aliases use this identity along with pathname information to find files and folders on the same volume. Beginning with Mac OS X 10.2, aliases reversed this search order by using the pathname first and unique identity second. This means that if you move a file and replace it with an identically named file, aliases to the original file now point to the new file. Similarly, if you move a file on the same volume (without replacing it), aliases use the unique identity information to locate the file.

When a file or folder moves, the alias may update either its path information or unique identity information to account for the change. If a file moves somewhere on the same volume, the alias updates its internal record with the new path information for the file. Similarly, if the original file is replaced by a file with the same name, but a different unique identity, the alias updates its internal record with the unique identity of the new file.

Create Symlinks

Let’s say you have two partitions or drives – “X” containing OS X and “Classic” containing OS 9. To navigate to “X”, you simply type

cd /

To navigate to “Classic”, though, you’d have to type

cd /Volumes/Classic

If you have many partitions and/or drives, and you are trying to manage many files across them, it can get annoying to type “/Volumes” every time. Moreover, you cannot simply create an Alias to the drive/partition to accomplish this because the command line utility “cd” does not handle Mac OS aliases properly.

To create the link, simply type the following:

cd /
ln -s /Volumes/Classic/ Classic

That’s it. ln -s makes a new file called Classic which points to /Volumes/Classic/. To see that this worked, you can simply type:

ls -la | more

You should see a line with the following text:

Classic -> /Volumes/Classic/

The arrow shows you exactly what you did… A file named Classic points to /Volumes/Classic.

Links have many possibilities around the filesystem. You can read more about them in the man page. While command line utilities cannot recognise Mac OS aliases, the Mac OS Finder will recognise symbolic links you create in the terminal.

No comments yet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

revia in alcoholism