How to Create Symbolic Links at Command Line of Mac OS X
A symbolic link created at the command line allows a linked object in the file system to point to an original object in a different location. In this way, symbolic links behave much like an alias does in the Mac OS X GUI, except that the linking and reference between files or folders is done at a lower level, and thus can be pointed directly to by various applications or user purposes. This can be useful in many situations for advanced Mac users, from providing easier access to a particular location, to offloading an application folder to another hard drive, and much more.
To make and set a symbolic link at the command line in Mac OS X, you’ll want to use the ln command with the -s flag, without the -s flag a hard link is set, which is not what we’re looking to do here. Launch the Terminal to get started.
How to Make a Symbolic Link
The basic syntax for creating a symbolic link (or soft link) is as follows:
ln -s /path/to/original/ /path/to/link
That will point /path/to/link to the original location, in this case /path/to/original/
Example Syntax for Making Soft Links at the Terminal
For example, to create a symbolic link for the user Downloads folder which links it to a directory on a separate mounted drive, syntax may look like the following:
ln -s /Volumes/Storage/Downloads/ ~/Downloads/
That will link the active users ~/Downloads/ folder to a directory named “Downloads” on the mounted drive called “Storage”. If such a directory and drive existed, this would basically allow all files that would typically appear in the user downloads folder to go to the other mounted volume instead, essentially offloading the storage burden to that separate drive, while still preserving the appearance of a ~/Downloads/ folder for the user. As mentioned before, this behaves much like an alias.
Another example would be to offer easier access to an otherwise buried binary by linking the command to /usr/sbin/
sudo ln -s /A/Deeply/Buried/Path/ToApp.framework/Resources/command /usr/sbin/commmand
This would allow the user to type ‘command’ and access the binary, without having to prefix the command execution with the entire path.
Soft links have tons of potential uses, and if you’ve been a longtime reader of OSXDaily you’ve undoubtedly come across them before in other articles, from gaining easier access to the powerful airport command, placing mounted NTFS volumes onto the desktop, to moving local iTunes iPhone backup folders to external drives, to adding a Trash can icon to the user desktop like retro Mac OS versions, or even placing an application cache folder onto a RAM disk for ultra-fast data access and caching. The practical uses are countless, and making symbolic links will work in any unix OS, so beyond Mac OS X you could apply the same idea to linux or FreeBSD.
How to Remove a Symbolic Link
Of course, created symbolic links sometime need to be undone. This is easy with rm, or by using the ‘unlink’ command as follows:
Essentially this is removing the tiny file (again, like an alias) that references the symbolic link to the original item.
Unlinking a symbolic link will not delete any files or folders other than that defined link, it simply removes the reference from the linked item to the original item.
Know of any particularly great uses or tricks with symbolic links? Let us know in the comments!