How to Create Symbolic Links at Command Line of Mac OS X

Aug 6, 2015 - 8 Comments

Terminal in 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 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:

rm /path/to/symlink

or

unlink /path/to/symlink/

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!

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

8 Comments

» Comments RSS Feed

  1. Wharf Xanadu says:

    You can leave off the -s for a hard link too

    • CVBruce says:

      The main difference between a hard link and a soft link is that hard link are between items on the same volume, while soft links don’t have that restriction.

  2. Mark Simon says:

    This technique can be used to move your iTunes (or other hard-coded) library to an external drive:

    1. Move ~/Library/Application Support/MobileSync
    to an external drive.
    2. sudo ln-s ~/Library/Application Support/MobileSync /Volumes/whatever/MobileSync

    Of course (a) the orginal folder needs to be removed and (b) you definitely need the -s option, since folders can’t be hard-linked.

  3. Jan says:

    Your example

    ln -s /Volumes/Storage/Downloads/ ~/Downloads/

    didn’t work for me.

    That trailing slash on ~/Downloads/ shouldn’t be there.
    And before you can make that symlink the original Downloads folder must be removed, like so: sudo rm -rf ~/Downloads/

  4. Hans Douma says:

    For the command syntax you say
    ln -s /path/to/original/ /path/to/link

    but in the example you reverse the parameters:
    ln -s /Volumes/Storage/Downloads/ ~/Downloads/

    Shouldn’t it be:
    ln -s ~/Downloads /Volumes/Storage/Downloads/

  5. Traveller says:

    I’m trying to use a symbolic link to deal with an issue where a python application is having trouble with directories where the name has spaces in it. Specifically my root volume name.

    I execute
    sudo ln -s “/Volumes/Data NAS 6/Mediastorage/Tivo-Shows” /Volumes/Tivo

    Everything works correctly and doing cd /tivo drops me to the my Tivo-shows folder.

    But when OS X performs a restart, the link is removed from the Volumes folder. Is there a way to maintain this link in the Volumes across restarts?

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates