Linux doesn’t have shortcuts, but it has Symbolic Links – or Symlinks, as they are usually called. But what exactly are symbolic links? And how do you create one? Let’s find out.

We will be trying out the commands on an Ubuntu installation, though the syntax works the same on all Linux distros. Interestingly enough, symlinks can also be created on a Mac PC.

Table of Contents
    How to Create Symbolic Links (Symlinks) in Linux image 1

    What Are Symbolic Links?

    If you want to be able to access a file from multiple locations, simply copying it over is not a good solution. You are wasting disk space, not to mention making it difficult to track changes due to there being multiple files.

    In a GUI-based operating system, you would get over this by creating shortcuts. For a CUI-based OS (like any Linux distro), the answer is a symlink. Even Microsoft Windows can leverage symlinks in some situations.

    A symlink or a symbolic link is just a file pointing to another file or directory – even in a completely different file system or partition. Opening the symlink gives you access to the same original file, with any changes reflected in the main file as well.

    This also goes for permissions – using chmod to change the permissions of a symlink file will actually modify the permissions of the target file.

    Types of Symlinks

    Symbolic links are also known as soft links. As the name might suggest, it isn’t the only type of file-linking system on Linux. Hard links can be used as an alternative to symlinks in certain scenarios, though comes with a number of restrictions.

    How to Create Symbolic Links (Symlinks) in Linux image 2

    To begin with, hard links are simply file names pointing to the same inode. While this makes them very efficient in terms of storage and access speed, it also limits them to files in the same file system or partition. And due to a quirk of how folder access works in Linux, you cannot use hard links for directories either.

    Soft links or Symbolic links, on the other hand, are more like desktop shortcuts. Each soft link is essentially a small file that points to the original data, without actually being directly linked to it. This means soft links can easily point to files and directories on other partitions and file systems as well without any restrictions.

    Hard Links VS Soft Links – Which Type to Use?

    Soft links are the most versatile form of links since they can be used on both files and directories across multiple file systems without any restrictions. But why then do we even have hard links? Do they afford you any advantages over symlinks?

    Well, on paper, hard links are more efficient. Because they use the same inodes, they do not take up additional disk space and can be accessed much faster than soft links. This is why many system administrators will recommend using hard links whenever you can.

    That being said, however, the cons of using symbolic links are minimal. Modern computers especially don’t really care about a few kilobytes of extra storage, and unless you are filling the system with millions of soft links, the additional file size rarely matters.

    Creating Symbolic Links

    Symbolic links – or rather, file links in general – are created using the ln command. By default, the command creates hard links. To create a symbolic link, you need to append the -s flag to the ln command.

    The syntax is simple enough. The ln command takes two parameters – the path of the file (or folder) to be linked to, and the path of the link file to be created.

    Like this:

    ln -s user_names.txt names

    How to Create Symbolic Links (Symlinks) in Linux image 3

    This will create a symlink called names connected to the user_names text file. To see if the symbolic link was actually created, just use the ls command.

    How to Create Symbolic Links (Symlinks) in Linux image 4

    As you can see, Ubuntu shows different colors for different categories of files, with directories denoted by dark blue and symlinks with a lighter blue color (cyan, actually).

    We can use the same syntax to create a symlink for the folder as well:

    ln -s examples example_folder

    How to Create Symbolic Links (Symlinks) in Linux image 5

    For creating symlinks to files and folders in other directories, simply use their full path. For example:

    ln -s examples/example3.txt distant_example

    How to Create Symbolic Links (Symlinks) in Linux image 6

    Limitations of Symlinks

    Symbolic links are a great way of connecting files across your system without having to resort to making copies, but they have their own limitations. Yes, even though soft links are less restrictive than hard links, there are still some things to keep in mind.

    First, the ln command doesn’t actually verify the links created. This means it is possible to enter a filename that doesn’t actually exist, and you will not get any errors. The only way to confirm the link is to try using it and see if it leads to the correct file.

    Second, while accessing or changing the permissions of the symlink changes the properties of the original file, the same doesn’t go for file deletion. You can safely remove the symbolic link without affecting the linked file or directory.

    Creating Symlinks in Linux

    The biggest pain in using the terminal to create symbolic links is the difficulty in accessing important files spread out across various directories. The smart solution is to create symbolic links to such files from your home directory itself, like desktop shortcuts.

    And unlike hard links, symlinks can be made easily without any restrictions, even for connecting to directories or files in different partitions and file systems. Keep in mind that this also means that a symlink might point to a non-existent file – you will have to try accessing the link to verify if the file exists.

    Leave a Reply

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