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

Aug 6, 2015 - 11 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 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:

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!

.

Related articles:

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

11 Comments

» Comments RSS Feed

  1. Patrick says:

    Is creating a symbolic link for a parent folder sufficient for an application to find any subfolders within that parent/symbolic link?

    When moving a folder to an external drive and creating a symbolic link, for example “/user/Music/iTunes” is it necessary to mirror the directory structure on the external drive, or does that not matter? For instance, could I move the iTunes folder to “/external HD/iTunes” or would it need to be structured as “/external HD/user/Music/iTunes”

    Asking because I moved some key folders to an external drive, and the application looking for stuff seems to be finding SOME not ALL of the redirected files. I’m using Apple Logic.

  2. 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?

    • Ben Wiles says:

      The problem is that you are putting the symlink in the Volumes folder, which is a special folder used by the system to mount filesystems (e.g. external drives), so the system is removing the symlink on restart.

      Try creating a symlink on the root (top folder) of your boot drive e.g.

      sudo ln -s ā€œ/Volumes/Data NAS 6/Mediastorage/Tivo-Showsā€ /Tivo

      That should stay in the same place.

  3. 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/

  4. 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/

  5. 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.

  6. 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.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site