How to Create Symbolic Links in Ubuntu Linux

If you’re wondering what symbolic links are and how to create symbolic links in Ubuntu, you’ve arrived at the right tutorial. Even though this is a very simple concept, symbolic links are very useful when working with Linux.

The reason why symbolic links are necessary is because of the fact that everything in Linux is a file. A file points to specific nodes on the filesystem and a symbolic link points to the location of the file.

What is a Symbolic Link?

If you’ve used Windows a lot, you’ve certainly come across shortcuts to files and programs that are placed on your desktop for convenient access. When you create symbolic links in Ubuntu or Linux, you create a shortcut that simply points to the location of another file.

There is another type of link in Linux which is called a hard link. In case of a hard link, you are directly pointing to the inode of the file instead of the file path.

The below illustration will make it very easy to understand.

create symbolic links in ubuntu - Illustrated
Symbolic Links Vs Hard Links Illustrated

Create Symbolic Links in Ubuntu Linux

Let’s understand how we can create symbolic links in Ubuntu Linux. The process is quite simple but we’ll cover the topic in as much detail as possible. The command used to create symbolic links is ln.

Since we edit the apt sources.list file pretty often, let’s create a symbolic link to the apt folder.

root@ubuntu:~# ln -s /etc/apt/ apt
create symbolic links in Ubuntu
Symbolic Link Apt Folder

Now we can directly cd into the apt folder from our home directory.

Cd Apt Symbolic Link Ubuntu
Cd Apt Symbolic Link Ubuntu

Isn’t this so convenient? Well, it sure is. But what happens if we create a direct symbolic link to the sources.list file and then edit the symbolic link. Let’s give it a try and see what happens.

Editing Symbolic Links

We’ll create a symbolic link for the sources.list file and append a line with the use of the echo command.

root@ubuntu:~# ln -s /etc/apt/sources.list sources
Symbolic Link Sources File 1
Symbolic Link Sources File
root@ubuntu:~# cat sources
Cat Command On Symbolic Links
Cat Command On Symbolic Links

In the above example, I was able to easily view the contents of the /etc/apt/sources.list file using the cat command on our symbolic link.

But can we edit the actual file by editing the symlink? Let’s give it a try.

root@ubuntu:~# echo "Appending a test line" >> sources
root@ubuntu:~# tail -n 1 sources
root@ubuntu:~# tail -n 1 /etc/apt/sources.list
Appending Line To Symlink
Appending Line To Symlink

We used the tail command to output only the last line in the file since we simply appended to the existing sources.list file. As you can see above, the line was appended to the original file too.

Similar to Windows shortcuts, symlinks allow you to edit the original file by directly editing the symlinked file.

Does Source File Get Deleted If Symbolic Links Are Deleted?

No. the source file is unaffected even if the symbolic link pointing to the file is deleted. This is obvious because by deleting a symlink, all we are doing is deleting the link pointing to the source. The source file doesn’t need to get affected by this action.

Conclusion

In this simple tutorial, we covered how to create symbolic links in Ubuntu Linux. You should now be able to create “shortcuts” to files in the Linux filesystem. If you’d like to read a bit more about the ln command that was used in this tutorial, here’s the Wikipedia page for the command.

I hope this tutorial proved useful to you. Feel free to comment down below if you have any questions!