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

Tips

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.


Products You May Like

Leave a Reply

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