A brief tutorial on symbolic links

Nov 06, '01 10:29:06AM

Contributed by: jmil

OS X's file structure mounts all partitions under the "/Volumes" directory at the root level of the filesystem. However, when navigating the filesystem with "cd" and other commands, it can be annoying to type "/Volumes/volume_name" each time you want to access a different partition. To learn about symbolic links and use them to add shortcuts at the root level of your filesystem, read the rest of this article. This assumes you are moderately comfortable in the Terminal, and that you have administrative privileges.

What is a Symbolic Link?
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." (If you want to read more about it, type man ln in the Terminal.)

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. However, a symbolic link will solve the problem.

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
(note the "|" character is called "pipe" and is found above the "Return" key). 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, and I encourage you to read more about them in the man page. While command line utilities cannot recognize Mac OS aliases, the Mac OS Finder will recognize symbolic links you construct in the terminal, or via Third-Party Utilities such as SNAX so you can use links just like aliases in the Finder.

Jordan

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2001110610290643