Overview

Hi there fellow Linux learners! Hope you’re ready to expand your knowledge about Linux. Today’s topic is how to create symlinks in Linux, which is short for symbolic links. There are two different kinds of links:

  • soft links
  • hard links

In this article we’ll focus on soft symbolic links. Hard links will be on menu some other time. Don’t be intimidated by its name, you don’t have to write symbols, neither remember or decipher ones. Soft symbolic links is just the fancy name for shortcuts. As simple as that. To create symlink you’ll have to use ln command (short for link – makes sense, eh?).

Full command to create symlink to a file looks like this:

ln –s [target file] [symlink file]

where –s stands for symbolic. So, example of creating a symlink would look like this:

ln –s my_file.txt my_symbolic_link.txt

For testing purposes, I switched to Desktop folder, created test.txt file and then created symlink for it by typing:

ln –s test.txt symlink.txt

And when I type ls I can see that it’s been created.

create symbolic links in Linux

Now, for you to better understand the relation between file and symlink, we’ll type the following:

ls –l symlink.txt

Create symlink to a file in Linux

Pay attention to part where it says symlink.txt -> test.txt, it clearly shows that symbolic link that we created is pointing to the file test.txt. Even though this articles are oriented towards terminal, this time I’ll make an exception just to show you what symbolic links look like in GUI.

If you wondered can a symbolic link be created to a folder, here’s your answer – yes, it can. Let’s say you want a quick access to your folders containing pictures, movies or music on your desktop, so you’ll make a shortcut or to be precise – symbolic link to your folder. And the procedure is quite the same as when creating symlink to a file:

ln –s /home/orby/Pictures ~/Desktop/my_pics

create symbolic links in Linux

Symlink to a folder has been successfully created and just like with previous example I’ll show you the result in GUI.

Create symlink to a directory in Linux

By now you’re probably wondering how to overwrite your symlink because you made some changes and it needs to be updated. For this purpose I created another file named test2.txt and I’ll try to link it to my symlink. I’ll use the same command as I did the first time:

ln –s test2.txt symlink.txt

create symbolic links in Linux

Well this is kind of discouraging. But, do not despair my fellow Linux learners, it’s time to use some magic powers again 

This time, along with –s, we’ll add –f (force) command. But, remember, with great power comes great responsibility. So, our command will now look like this:

ln –sf test2.txt symlink.txt

Don’t be tempted to abuse the newly gained powers, force user.

When it comes to folders, you have almost the same situation, but with some differences. I’ll try to explain through examples. First, I’ll create two folders, Fox and Eagle. Then, I’ll make a symlink to one of them, let’s say Fox, and I’ll name it Rabbit.

create symlinks in Linux

OK, this should be well known by now. Now, if you decide to change symlink and switch it to Eagle folder, just like as it was with files, you’ll use command:

ln –sf ~/Desktop/Eagle Rabbit

What happened? Unlike forcing the change of path of files, when it comes to folders it didn’t do what was expected. Instead, Rabbit is now both shortcut for Eagle and Fox folders. You can see that it even created symlink of Eagle inside Fox folder.

create symlinks in Linux

To do the simple overwrite of symlinks to a folder, we’ll add yet another option in our commands:

 ln –sfn ~/Desktop/Eagle Rabbit(-n stands for –no difference)

Replace symbolic link in Linux

Oh yes, we got it this time. Now everything is on perfect order.

We’re near the end of this article. The last thing that I need to show you is how to remove symlinks. You have two choices when it comes to that. Either you use command rm (remove) or command unlink.

rm symlink or unlink symlink are the commands and the choice is yours, depending on your preference and requirements and in some sence it matter with which option you go with.

Remove symlink in Linux

Why is it important? Mainly because how they work and how they handle files and symlinks. Both rm and unlink can be used to remove symbolic links in Linux and Unix-like systems. They behave diferently how they work and when they can be used:

  • rm:

    • When used on a symbolic link, rm deletes the link itself, not the target file or directory.
    • If the symbolic link points to a directory, rm won’t follow the link unless specific options (e.g., -r) are provided.
    • Can work on multiple symbolic links at once, e.g., rm symlink1 symlink2.
    • If a symbolic link points to a directory, rm will remove the link, not the directory, by default.
    • However, rm -r can recursively remove directories, including the content of a directory if a symbolic link points to it (dangerous!).
    • Can be configured with options like -i or -I to prompt before deletion. Example: rm -i symlink
  • unlink:

    • Removes the symbolic link itself, not the target.
    • Works only on one link at a time.
    • Does not have any additional options; it’s a straightforward command.
    • Cannot remove directories, symbolic links to directories, or the contents of directories.
    • It’s limited to removing a single file or symbolic link only.
    • Does not provide interactive prompts. It removes the target immediately if possible.

Summary

Here we covered many methods of managing the symbolic links and with these techniques, you’re well-equipped to handle symbolic links confidently and efficiently in any Linux environment. These can probe to be useful and powerful and it can also make it easier to organize and access resources across your system and ehhance your workflow.

I hope you found it useful and i thank you for your time…