Soft Links in Linux – The Complete Reference

Filed Under: UNIX/Linux
Soft Links Featured Image

A link in Linux systems are pointers to a file or a directory. There are two types of links in Linux, namely soft and hard links.

In this article, we will examine soft links in detail. Similar to shortcuts in Windows, soft links, also known as symbolic links, point to a file without storing the file’s contents. Any changes made to either the file or the soft link, are reflected in both the versions of the file.

Representation of Soft Links in Linux

After understanding the concept of soft links, we need to know how to spot a soft link in a file-system.

Color scheme

The 'ls' command provides a color scheme for every different component in the Linux file-system. Soft links are denoted by 'cyan' color.

Soft Link Ls Cyan
Soft links – ‘program’ and ‘desktop’

In the above output, 'desktop' and 'program' are soft links. It may happen that some systems have modified their default color schemes and therefore are unable to figure out the soft links.

Using the ls -l command, we can clearly find out links present in a directory.

Soft Links in Linux Ls L Edited 1
Contents of the current directory

Not only does it specifies links in the directory, but also displays the original file location or directory for a soft link.

GUI representation

Similar to shortcuts in Windows, Linux provides a hint in the icons of soft links. The GUI icons for soft links contains arrow signs at the bottom-right corner.

Soft Link Gui Edited
GUI Icons for soft links

It is quite evident from the figure that soft link named 'desktop' is a pointer to a directory whereas 'program' points to a ‘.cpp’ file.


How to create a soft link in Linux?

Now that we have seen the methods of spotting a soft link, we will learn how to create soft links in Linux. This is done with the help of 'ln' command.

ln -s <PATH>/<ORIGINAL_FILE> <LINK_NAME>
Soft Link Create
Creating a soft link

The 'ln' command is specifically used to create a link in Linux. The '-s' option used in the above command represents the creation of a soft link.

Using the 'ls -l' command, we can check whether the creation of a soft link was successful or not.


Editing the original file

Since a soft link is just a symbol for the original file, any changes made in the original file will be reflected in the soft link as well. Let us demonstrate the changes:

  • Original File – “my_program.cpp” in the Documents folder
  • Soft Link – “program” on the Desktop

We will use the sed command to edit the original file.

sed -i "s/main/disdain/g" my_program.cpp
Soft Link Edit Original
Editing original file

The above command simply finds all the occurrences of the word “main” and replaces each one of them with the word “disdain”.

The 'program' file present on the Desktop is a soft link, therefore has to reflect the changes made in the original file.

Soft Link Edit Original Done
Changes reflected in the soft link

Editing the Content through a Soft Link

Editing the contents of a soft link reflect changes in the original file as well. This can be demonstrated by the following screenshot:

Soft Link Edit Link
Editing a soft link

As previously mentioned, 'program' is a soft link. Using 'echo' command, we append the word “Edited” to the soft link. We can clearly see the changes that happen in the original file 'my_program.cpp' as well.

Note: While editing the soft link we did not use 'sed -i' command, as in the process, the soft link is removed and a new file is created with the same name.

We will see later that removing the original file and placing it back, preserves the link.


Identify Broken Soft Links in Linux

The soft links break when we delete the original file. When using the ‘ls’ command, broken links are displayed in red color with a black background.

Soft Link Broken
Broken Link

In the above figure, we move the original file to the current directory. When we remove the original file from their original location, we can see the change in color on a soft link.


Fix broken links

Every soft link points to an originating file. We can easily fix a broken link by replacing the original file with another file of the same name. I’ve demonstrated the same below.

Soft Link Unbreaking
Fixing a broken link

Removing a soft link in Linux

The easiest way to remove a soft link is using the 'rm' command followed by the link name.

rm <LINK_NAME>
Soft Link Rm
Removing link using ‘rm’

There is one another way to remove links in Linux. It is done by 'unlink' command.

unlink <LINK_NAME>
Soft Link Unlink
Soft Link Unlink

Soft link of a soft link

Using the 'ln' command, let us create a soft link to our previously created soft link.

ln -s <LINK_NAME> <NEW_LINK_NAME>
Soft Links
Soft link of a soft link

It is quite clear that these links form a chain. A change in any one of the links will be reflected in every one of the files.

Since, the continuous links form a chain, removing any of the in-between links, will break the child links. For instance, if we remove the first soft link 'program', the child link will break.

Soft Link Breakchain
Breaking the chain of soft links

When we break the link in the middle, the complete chain breaks.


Conclusion

Soft links are a common Linux feature that links libraries and files in Linux file-systems. This article covers up the creation, properties, and removal of soft links in Linux.

We hope the article was easy for you to understand. Feel free to comment below for queries or suggestions.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages