How to use symbolic links on Linux

Symbolic links (or Symlinks) are files that act as shortcuts or references to another file or directory. Although different, roughly speaking, we can think of symbolic links as being similar to software shortcuts on the desktop: by clicking on them, we run a program that is stored elsewhere on the computer and, sometimes, even on another machine. But everything works so transparently that it looks like we’re clicking on the original executable.

The use of symbolic links in Linux is very common: different programs tend to use links to libraries instead of replicating the file in another directory. This is also a good way to save disk space and system resources.

How to create a symbolic link

It is very easy to create a symbolic link on Linux. Just use the ln command with the following syntax:

ln -s path / file original path / symbolic links [symlink linux]

Say you want to create a link in your home directory to the /var/log/boot.log file . In this case, the command would be:

ln -s /var/log/boot.log / home / user / newfile

When a symbolic link is created, it starts to reference the original file. This means that when reading or editing the symbolic link, you will be reading and editing the original file. However, if you delete the symbolic link, be aware that the original file will remain intact. The opposite is no longer the case: if the original file is deleted or renamed, the symbolic link still exists, but is pointing nowhere (broken or dangling symlinks).

Read: How to find the largest files on Linux

In order to find broken symbolic links, execute the following command (for symlinks under a given directory):

find /path_to_directory -xtype l

Now to delete these broken links, invoke the command :

find /path_to_directory -xtype l -delete               [remove symbolic link]

Inodes and hardlinks

In the file system of Linux and other Unix-based Operating Systems, an inode is a data structure that represents a file or directory. Therefore, a file must have a unique inode within a given partition. This index stores properties such as the file’s creation date, physical block where it is located on the hard disk, etc.

When creating a symbolic link (with the -s parameter ), the original file and links have different inodes which can check with the command ls -i file .

Read: How to manage permissions in Linux, guide for beginners

However, ln allows you to create another category of links, which has stronger relations with the original file. Because they share the same inode, the so-called hardlinks have very clear differences from symbolic type links:

  • They can only reference files, never directories;
  • They cannot reference files on other volumes or partitions;
  • Hardlinks continue to work even if the original file has been deleted;
  • Hardlinks refer to the inode of the original file. Unlike softlinks, which reference file and directory names, hardlinks refer to a physical position of the partition.

To create a hardlink, just follow the syntax of the ln command already presented in this tutorial, but without the -s parameter :

ln path / original-file path / link

This type of links can be used in several practical scenarios, one of the most common being the backup : if the same file appears in more than one folder, for example, just copy it once and “replicate” it through hardlinks. If the original or link is updated, so will all of them and, in case the original is removed, the links are still working. Again: saving disk space for system resources.

To delete or remove a symlink, run the rm command as follows:

rm symlink_name                                          [remove symlink linux]

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.


Nikolaus Oosterhof

Nikolaus has a degree in software development. He is passionate about gadgets with a screen, nostalgic for phones, a retired gamer and open source programmer. He likes also to write about macOS and Windows. design web pages and debug long programs!

Leave a Reply