What Is a Symbolic Link (Symlink)? How to Create One in Linux
Whatsapp Pinterest
Advertisement

As a computer user, if you had to define a shortcut, you’d probably say it’s a pointer to a file, folder, or an app, right? That’s correct.

But that short definition doesn’t tell the whole story at all. It implies that all shortcuts are the same when they’re not. You have nearly a handful of shortcut types. We’ll focus on the symbolic link below. It’s also called a symlink or a soft link, and we’ll use the terms interchangeably.

Let’s see what is a symlink, how to create a symlink on Linux as well as macOS and Windows, why you need this special type of shortcut, and more.

What Is a Symlink?

It’s true that a symlink is a shortcut file. But it’s different from a standard shortcut that, say, a program installer has placed on your Windows desktop to make the program easier to run.

Sure, clicking on either type of shortcut opens the linked object, but what goes on beneath the hood is different in both cases as we’ll see next.

While a standard shortcut points to a certain object, a symlink makes it appear as if the linked object is actually there. Your computer and the apps on it will read the symlink as the target object itself.

On macOS, you get proof of this in the form of a “file exists” message that appears when you try to create a symlink in the same location as the original object. Even if you try to move the symlink to the same location after creating it elsewhere, it gets renamed as a copy.

File exists message while creating a symlink in Terminal on macOS

Symlinks vs. Standard Shortcuts

Let’s say you have a certain folder on your hard disk that you’d like to sync with Dropbox without moving the folder itself to Dropbox.

In this case, creating a shortcut to the folder in Dropbox is pointless. The shortcut will work on the device on which you have created it. Dropbox will sync the shortcut, too. But, the synced shortcut file is invalid when you access it from a different computer, i.e. it leads nowhere.

Now, if that shortcut were a symlink, you wouldn’t face this problem. That’s because Dropbox reads the symlink as the actual folder and as a result, syncs the data from that folder. You can then access the folder and its contents on all your devices that have Dropbox sync enabled, even though the original folder is not a part of your Dropbox.

Keep in mind that whether it’s a regular shortcut or a symlink, deleting it will not impact the original object in any way.

Why Do You Need Symlinks?

In general, it’s a good idea to create symbolic links instead of shortcuts when you want to:

  • Access a file from multiple locations without creating copies and without using much disk space. (Symlinks are only a few bytes in size.)
  • Maintain different versions of a file while ensuring that any pointers to it always lead to the most recent or up-to-date version. (This works because a symlink remains active even when you replace the target file with a different file of the same name.)
  • Move data off your C:\ drive to, say, a secondary hard drive without disrupting system or app functions that need said data to be on the C:\ drive.

You’ll likely come across many other use cases for symbolic links.

How to Create Symlinks

You can create soft links using the terminal or command line. We’ll get to the point-and-click tools later if you’re uncomfortable fiddling with the terminal.

On Linux and macOS

Terminal command for creating a symbolic link on macOS

On Linux, you can create a symbolic link for a file or folder with this terminal command:

ln -s [/path/to/file] [/path/to/symlink]

The same command works on macOS too, since macOS is a UNIX-based operating system like Linux.

See the screenshot above for a sample command.

The native file manager in certain Linux desktop environments lets you create a soft link via the right-click menu, so you might want to check if your file explorer app has that option.

The popular Nautilus file manager, which comes bundled with various Linux distros, had a Make Link menu option that has now gone away. But you can still create a symlink in Nautilus by holding down the Ctrl and Shift keys and dragging the target file to the location where you want the symlink to show up. Don’t worry, the original file will stay put.

On Windows

You’ll need to open a Command Prompt window as an administrator and type in the following command to create a symbolic link:

mklink [/path/to/symlink] [/path/to/file]

For symbolic links to directories, you have to tweak the command a bit using the /d flag:

mklink /d [/path/to/symlink] [/path/to/file]

If you don’t want to work with the command line, you can use a graphical tool called Link Shell Extension to create symbolic links. It’s one of the best Windows File Explorer extensions for file management.

Note: The system won’t prevent you from creating a symbolic link within a symbolic link, but it’s best to avoid doing so. Otherwise, you’ll create an infinite loop that can cause issues for system-wide services like antivirus scanners.

Symlinks vs. Aliases on macOS

Symlink and alias for a file in Finder on macOS

If you’ve ever created aliases on macOS, you’ll notice that they behave much like symlinks. Both types of shortcut reference the pathname of the linked file or folder.

The difference is that the alias also marks the linked object with an identifier called inode (index node). This identifier is unique to the object and follows it around the file system.

That’s why the alias will work fine even if you move its target to a different location. Try that with a symlink and you’ll encounter an error. (You can move the alias and the symlink themselves without any problems, unless you’re dealing with system-protected files.)

Of course, both types of shortcuts will prove useless if you delete the original file or rename any of the folders higher up in the hierarchy.

Can’t tell the difference between an alias and a symlink because you have removed the alias tag from the filename for the alias?

Open up the file inspector or Get Info panel for each shortcut and look at the file size under the General section. If it says (zero bytes on disk), you’re dealing with a symlink.

Symbolic links (in the current folder) will also reveal themselves when you use this terminal command:

ls -la

The command works on Linux too, and you’ll see the symbolic link point to the location of the original object.

Symlinks Are Better Than Shortcuts

Symbolic links might seem confusing initially, but if you take the time to understand them, you’ll realize that they’re quite easy to use after all!

You can even create symbolic links on Android with Termux, an app that lets you use the Linux command line. And did you know that you can share Google Drive files more easily with symbolic links Make Google Drive Files Easier to Share With Symbolic Links Make Google Drive Files Easier to Share With Symbolic Links Want to make copies of a file that all stay synchronized when changes are made to the master file? Google Drive makes it easy to do that. Read More ?

Explore more about: Symbolic Link, Terminal.

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. tung
    August 21, 2019 at 2:19 am

    good

  2. Mike Walsh
    August 14, 2019 at 2:37 pm

    Many of us who run Puppy Linux use sym-links regularly. It enables us to run multiple Puppies on the same system, each one being able to access common data directories, apps AND utilities from all Pups; not only that, but also common .cache & .config directories/files, so that, say, a single install of a given browser can be used across multiple OSs......with extensions, bookmarks, etc, remaining the same, regardless of access point.

    Myself, I also use sym-links to access common personal data directories from multiple OSs. That's probably one of the sym-link's more well-known uses.

    • Akshata Shanbhag
      August 15, 2019 at 7:34 am

      Thank you for pointing out that use case, Mike!

  3. v
    August 11, 2018 at 2:49 am

    I did this on Nextcloud. Symlink didnt worked and hardlink did! WTF? (Linux>Windows)

  4. Stephen
    March 31, 2016 at 2:56 pm

    Use case: Multiple copies of the same file that serve a purpose in their respective locations. How can they be maintained? Hard links?

  5. The Bishop
    February 20, 2016 at 2:58 am

    The terminal is not necessary on all operating systems. I use a Linux system, Tahrpup_64, that uses ROX-Filer as the file manager. To copy, move, or create relative or absolute symbolic links you just drag and drop from one directory window to another. When you release the mouse button in the destination window, a menu pops up asking which of the 4 options you want to do. Easy, peasy.

  6. moorpipe
    September 7, 2013 at 11:38 pm

    I'm sorry to say but your description of MKLINK is confusing. Perhaps you're not quite familiar with the existing linking mechanisms. Please refer to an explanation of MKLINK here: http://technet.microsoft.com/en-us/library/cc753194(v=ws.10).aspx
    For a very good article on junctions, symbolic and hard links, please check out:
    http://comptb.cects.com/2268-overview-to-understanding-hard-links-junction-points-and-symbolic-links-in-windows

  7. Lazza
    August 29, 2013 at 7:54 am

    «technically speaking a symbolic link is for a directory, a hard link is for a file, and a soft link is another term for a shortcut»
    This is completely wrong. In any operating systems class they teach you that symbolic links are different from hard links, and you can create them both for files and folders.

    Also, you can create a symbolic link from Ubuntu graphically, by selecting something and choosing "link to desktop" (sorry, I don't know the exact wording in English) then you can move the new symlink in any folder you want, renaming it as you wish. I guess it's the same also for other distributions and DEs.

    • Danny Stieben
      August 31, 2013 at 5:06 pm

      I'm sorry if those happen to be wrong -- I'm sure I got the concept down, but the naming conventions are pretty difficult to differentiate. Haha.

    • Stephen
      August 3, 2017 at 6:42 pm

      You will find Google's definition of condescending below.
      "having or showing a feeling of patronizing superiority."

      In the future, use these two tips.
      1. Refrain from using hyperbole like we see in "completely wrong" and "any operating systems class."

      2. Lastly, it's peculiar seeing you chose to use the acronym DE but not OS. OS would have made "Any operating systems class" easier to read because you did not capitalize it as a proper noun and instead wrote it as a verb.

      3. Don't say "I guess" if you are correct.

      • Lazza
        August 3, 2017 at 6:56 pm

        Stephen yup, that definitions defines *your* reply to a comment left 4 years ago perfectly.

  8. Greg Webb
    August 29, 2013 at 4:38 am

    I think symbolic links are great BUT there is one downside to spoil the joy. Backup programs may not understand symbolic links, and further more users may not know what to do with them when they use a backup program that does.

    The issue is, should a backup program backup the files from the symbolic link? You may end up with TWO copies of the files at the end of the symbolic link.

    The issue is so complex that I only use symbolic links on very rare occasions. I once had lots, having discovered the advantages, but got badly burned with backups.

    • Danny Stieben
      August 31, 2013 at 5:05 pm

      I'm not quite sure I understand this. Unless you actually have two copies or you create a loop via symbolic links, it shouldn't back up two copies of the same file.

      • Greg Webb
        August 31, 2013 at 11:00 pm

        Hi Danny, Say you have two drives: C and D. On D you want to have a shortcut to C:\Program Files from D which you create with a symbolic link (I don't why you'd want to do this but it's useful for this explanation). Let's say the the path of the symbolic link is D:\My Programs\WinProgs. When you do a backup of the D drive, should the backup copy the symbolic link itself or the files represented by the symbolic link (ie the contents of C:\Program Files).

        I would argue that in some circumstances you want only the link and other times you'd want the contents of the linked to directory. It then comes down to:
        (1) what do you want,
        (2) will your backup program recognise the symbolic link and ask you what you want to do.

        Two copies: If you back up both C and D, you may end up with two copies of \Program Files.

  9. neo269
    August 28, 2013 at 1:28 pm

    I have created a symbolic link in Drop box. as follows

    Folder A is dropbox
    Folder B is folder which I want to sync with drop box
    So I create a symbolic link in Dropbox. i.e. \\A\B

    Now how do I update the Symbolic link folder B when actual folder B is updated?

    • Danny Stieben
      August 31, 2013 at 5:03 pm

      It should update automatically. The only issue that may appear is if you move folder B to a new location, because the symbolic link will still point to the old location. In this case, you'd simply have to delete the symbolic link and recreate it.

  10. Mac W
    August 28, 2013 at 5:16 am

    Mostly use symbolic link to have DropBox to sync folders outside the Dropbox-folder

  11. ReadandShare
    August 28, 2013 at 2:22 am

    Danny (or anyone else who knows):

    I have an app that writes data in its own folder -- with no option to write to my Documents folder instead.

    I like to have all my data in one folder (Documents) -- to make it easier and foolproof when backing up to an external drive.

    Would adding a symbolic shortcut in the Documents folder that points back to the app folder do the trick -- and allow for backing up as described above?

    • Peter E
      August 28, 2013 at 7:51 pm

      That's exactly how a symbolic link may be used. Without knowing what operating system you use, I couldn't tell you specifically, but it essentially goes something like this:

      The app writes data by default to /home/danny/Application/data in a file named profile.bin.

      Move 'profile.bin' to your Documents folder (eg /home/danny/documents/profile.bin)

      Navigate back to the application's folder, in this example: /home/danny/Application/data

      Next you would create the symbolic link IN that folder, such as: ln -s /home/danny/documents/profile.bin /home/danny/Application/data/profile.bin

      This follows the article's description for creating the symbolic link in the manner of ln -s [source file] [destination].

      Incidentally, when creating the link as above, you can choose any name for the link by choosing a different name in the destination path. EG: ln -s [/sourcepath/apples.doc] [/destinationpath/bananas.doc].

      This example uses the ubuntu terminal commands. you would need to find out the commands for your particularly OS.

      • Peter E
        August 28, 2013 at 7:53 pm

        incidentally, I mentioned you can change the file name of the symbolic link on creation of the link, but for your purpose, you must not, as the app you refer to wont be looking for your new filename, but the intended filename.

      • ReadandShare
        August 28, 2013 at 10:10 pm

        Ahhh... I am confused -- sorry, but I am quite unfamiliar with command lines. Given:

        1. source data file c:\user\dany\appdata\contacts manager\contacts.db
        2. destination: c:\user\danny\documents

        Do I need to create a file in the destination first? Or do I just type one big command line? If so, can you please show me the command line so I can type "as is"? Don't suggest any options or niceties for now -- just what's needed to establish the symbolic link. Thanks!

        • Peter E
          August 29, 2013 at 6:06 pm

          ok, so you need to use the make link command if you are using windows. To do this, you need to use the windows console.

          First:

          Navigate to C:Userdannyappdatacontacts manager, right click the 'Contacts.db' file and select cut. Next, navigate to C:userdannydocuments then right click and choose paste.

          Then:

          Click Start Menu and in the 'Run' box (or Search for files and folders box in windows 7) type cmd.exe.

          using your example of the file's location enter the following command:

          mklink /H C:Userdannydocumentscontacts.db C:Usersdannyappdatacontacts managercontacts.db

          Use at your own risk. you need to ensure you have the correct path information etc.. Please ensure you back up your data first and create a restore point so that you can undo any mistakes you make.

          For more information on symbolic links and a windows based GUI application for making symbolic links visit:

          http://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/

          (sorry to link a different tech website.. is this allowed MUO?)

        • Peter E
          August 30, 2013 at 9:21 pm

          sorry, for some reason the 's were removed from the path's in the above explanation but im sure you can see what to do.

  12. Rick
    August 27, 2013 at 8:13 pm

    I'm pretty sure this is the same thing. It works great on an Android phone for certain apps with large secondary files that insist on being in internal storage. Google Play Music is one in particular. If you pin music for offline use, it downloads them to internal storage and there is no option to move it elsewhere on newer phones (ironic that older ones do this by default, store pinned music to the sd card that is). But with a symbolic link, you can move the entire music folder to the sd card while the phone still thinks it is using internal. Very helpful if your phone only has a gig or two of internal storage space.

    • Danny Stieben
      August 31, 2013 at 5:01 pm

      I assume that requires a rooted device so that you can create the symbolic link in the first place, correct?

      • Rick
        August 31, 2013 at 9:13 pm

        I'm not 100% sure about that, but I assume so. I am rooted. If it is a system app, I would guess that you definitely need to be rooted to do this. If it's a user app, possibly not? I don't know. I did use Root Explorer to do it. I don't see the same "Create Shortcut" option in ES File Explorer, but some other file manager apps may have the same ability.

        Apparently, it doesn't work in all cases though. I tried to move my upload folder for my Eye-Fi camera card into the camera folder on my phone, so the pics would upload automatically into Dropbox, but it wouldn't let me create the shortcut back to the original location. If you aren't familiar, this SD card uses its own wifi to upload pics from my camera to the Eye-Fi app on my phone, where I can look at them better than on the tiny little camera screen. Then, as soon as I get home and on my wifi there, the phone automatically uploads them to the Eye-Fi application on my computer. Pretty nice! But I've been wanting to play around more with Linux, dual booting it on my desktop. Eye-Fi is for Windows (and maybe Mac). So I was hoping to use the automatic upload to Dropbox as a workaround to get to my pics in Linux , rather than having to boot into Windows every time I take some pictures.

        That leads me to another question. If I install Dropbox in Ubuntu, can I navigate it to use the Dropbox folder that already exists in the Windows file system? Will Linux and/or Dropbox let me do that? I know that you can tell Dropbox where to put its folder but I don't know if that works across two platforms on the same machine. Would a symbolic link be a way to do this? I certainly don't want two duplicate Dropbox folders on the same computer.

        • Rick
          August 31, 2013 at 10:39 pm

          That actually seems to have worked. I have my Dropbox folder inside my Documents folder in Windows. So I navigated to the Windows "Documents" folder from within Ubuntu. Then I created a link to that folder and moved it to the "Home" folder in Ubuntu. Then, in the setup for Dropbox, I told it to put the "Dropbox" folder inside the link to "Documents". It promptly told me there was already a folder named "Dropbox" there, and asked if I would like to merge the files. I told it yes, and everything is in the process of synching right now. Windows and Linux sharing the same Dropbox folder on the same computer. Now I just need to figure out the Eye-Fi problem.

  13. Peter E
    August 27, 2013 at 7:24 pm

    I use Ubuntu One to sync my files, particularly the photo's that I take on my mobile phone.

    I also use shotwell phot omanager on my Ubuntu build. When you open Shotwell, you can program it to check your 'Pictures' folder and update any pictures there, however, you cannot manually select any folder you wish. Therefore I created a symbolic link in my pictures folder to my ubuntu one mobile pictures folder.

    Now, when I take a photo on my phone, it is automatically synched with Ubuntu One, then when I get home and open Shotwell, it magically appears there too :)

    • Danny Stieben
      August 31, 2013 at 5:00 pm

      Great example! Thanks for sharing, Peter!