Hard Links and Soft Links in Linux

When it comes to links, we always get confused with symlinks, hard links, soft links symbolic links, etc. In this post, I will try to cover everything about these terms.

Table of Contents

Links in Linux/Unix are similar to shortcuts in Windows OS. When we “open” them, the original file being targeted is opened directly.

Links in Windows 10
Links in Windows 10

Links are of two types – soft and hard links. The soft links are also known as symbolic links and symlinks.

Soft LinksHard Links
The link and the linked file can be on different file systems (ex – the link on EXT4 and the linked file on NTFS).They cannot be on different file systems.
If the original file is deleted or its path is changed, the symlink will not work.The hard link will work.
We use the ln --symbolic (or ln -s) command to create soft links.ln command is used here without any flag.

We create a symlink using

~$ ln --symbolic <target> <link_name>

Where the target is the file or directory needed to be linked. The link_name is the name of the link.

One very good example in the X11 system is:

~$ ln --symbolic ~/.Xresources ~/.Xdefaults

The above symlink is recommended for all bare-bone Linux users. We need both the files ~/.Xdefaults and ~/.Xresources on different occasions (You might have noticed it while setting up URxvt). Instead of making them the same manually, we create a symlink like above.

You can also verify the creation of symlink by using ls -al command:

~$ ls -l --all

The arrow (->) symbol, just next to .Xdefaults, tells that .Xdefaults is a symlink.

arrow symbol in ls -al command showcasing symlinks
Arrow (->) symbol in ls -l --all command showcasing symlinks

Another great example is becoming prevalent thanks to the rampant usage of SSDs. Generally, SSDs are smaller and more expensive than HDDs. So we can store large directories containing movies (for example) on HDDs while linking them to SSDs.

~$ ln --symbolic /mnt/my_HDD/movies ~/Videos/movies

You can also notice that in the first example, the target (i.e. ~/.Xresources) is a file while in the second example the target (i.e. /mnt/my_HDD/movies) is a directory.

😃 Fun Fact: Linux treats directories, optical discs, CPUs, batteries, and everything else as files.

When there is already a symlink with the same name, the ln command throws an error. To overcome this error, use -f (or --force) flag. This will overwrite the existing symlink with the new one.

~$ ln --symbolic --force <target> <link_name>

An example used very often during Archlinux installation:

~$ ln --symbolic --force /usr/share/zoneinfo/GB /etc/localtime

We can do it in two ways:

The first one uses the rm command. Just remove it as you do with any file.

~$ rm <link_name>

The second one uses the unlink command.

~$ unlink <link_name>

Concluding Remark

Links in Linux alleviate the need to create duplicate files manually. At the same time, they provide a robust way of organizing your files. To know more about these, use the man ln and the man 7 symlink commands. And also, please provide me with your suggestions in the comment section below to improve this article.

Leave a Comment

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