Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Creating Symlinks on Linux

A symbolic link, also called a soft link, or a symlink, is a special file on a Linux system, that points to another file or folder on the system. These are a rough equivalent to a Windows shortcut, on a Linux system.

Difference between Symlinks and Shortcuts

A symlink does not act as a pointer to a different file, or a folder, unlike a shortcut. A symlink acts as if the object is actually there.

For example, we want to sync a folder to dropbox, but we don't want to move it to dropbox. Creating a shortcut would make dropbox refer to that file location, and hence if we access it on another system it would fail. But with a symlink, Dropbox thinks the file is present there, and syncs the folder contents, giving us access even on other systems.

Applications of Symlinks

  • Memory efficient shortcuts. Execute the same application from various folders, but consumes only a few bytes to make a symlink.
  • Moving of data, or applications to another drive, without disrupting work.

Creating Symlinks

To create a symlink from the command line, the basic command format is as follows.

ln -s <path to file/folder> <path of link>

ln, is the link command. -s is used to specify that our link is symbolic and/or soft. By default ln creates only hard links, so we need to remember to specify the -s flag. After that in order, is the source file/folder, and then the destination of the shortcut.

Symlinking a file

To symlink a file, for example, the .bashrc file in your ~ directory, to a .termrc file in the Documents/ directory, we run the following.

ln -s .bashrc Documents/.termrc

To check if a file is a symlink, running the command

ls -l Documents/.termrc

If the first column is an l, it means it is a link.

ln -s output, symlinking a file

Symlinking a folder

To symlink a folder, for example, to access ~/.local/bin/ (user local executables) in ~/Documents/projects/, we would run the following.

ln -s ~/.local/bin/ ~/Documents/projects/lbin

symlinking a folder

Removing Symlinks

Symbolic links can be unlinked in two manners. One is using unlink command.

Unlinking a Symlink

unlink has the following syntax.

unlink <path to link>

This will delete the link if the process is successful. A thing to remember is, even if the symlink is for a folder, we do not add the trailing '/', because Linux assumes it to be a directory, and unlink can't delete directories.

unlink a symlink

Deleting a symlink

Since a symlink is nothing but a special file on a Linux filesystem, it can be removed in the same a Linux file can. By using rm. Just like with unlink, the trailing '/' is not to be added if the symlink is that to a folder.

The syntax to rm (remove) a symlink is as follows.

rm Documents/projects/lbin

removing a symlink via commandline, rm command

The benefit of using rm over unlink is that you can simultaneously delete multiple links using rm.

Finding and Deleting broken Links

Sometimes there are too many links to keep track of, and we have modified our files various times, and we end up with dangling/broken links, due to missing files, or renamed folders, and other such causes. To handle this, we can find all broken links using the handy find program on Linux. find is a Linux utility that can search for symbolic links, and delete them as well, out of many other things.

To search for broken symlinks, use the command

find . -maxdepth 1 -xtype l -print # Max Depth to be used to find links, only in current folder, without recursive search
find . -maxdepth 1 -xtype l -delete # Delete after confirmation of files using -print

Hence, for example, the following commands were used to make a symlink and then move the original file, we get.

ln -s abc.cpp acbd
ll acbd # Check on link
mv abc.cpp a.cpp
ll acbd

We can see that the file acbd, is now a broken link. To search and remove it, we use the find command and -delete flag.

Find and deleting broken symlinks


This tutorial covered how to use the ln command, with the -s flag to create soft/symbolic links, and how to handle those links, using unlink, and rm. It also covers how to find any broken links due to mishaps, and delete them.