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?).
Create symlink to a file
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.
Now, for you to better understand the relation between file and symlink, we’ll type the following:
ls –l symlink.txt
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.
Create a symlink to a folder
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
Symlink to a folder has been successfully created and just like with previous example I’ll show you the result in GUI.
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
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.
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.
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)
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, you can’t miss with any of these.
I used both commands on symlinks created for this article, so you can see they’ve done the job. With the destruction of symlinks we came to an end of this article where we explained how to create symlinks in Linux but also but also covered how to remove symlinks and how to ovewrite them too.
Share your thoughts and ask questions in comment section. Until next time, keep you mind open to new things and be prepared to learn some new stuff.