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.
'ls' command provides a color scheme for every different component in the Linux file-system. Soft links are denoted by
In the above output,
'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.
Not only does it specifies links in the directory, but also displays the original file location or directory for a soft link.
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.
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 -s <PATH>/<ORIGINAL_FILE> <LINK_NAME>
'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.
'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
The above command simply finds all the occurrences of the word “main” and replaces each one of them with the word “disdain”.
'program' file present on the Desktop is a soft link, therefore has to reflect the changes made in the original file.
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:
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.
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.
Removing a soft link in Linux
The easiest way to remove a soft link is using the
'rm' command followed by the link name.
There is one another way to remove links in Linux. It is done by
Soft link of a soft link
'ln' command, let us create a soft link to our previously created soft link.
ln -s <LINK_NAME> <NEW_LINK_NAME>
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.
When we break the link in the middle, the complete chain breaks.
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.