Updated April 15, 2023
Introduction to Linux Symbolic Link
In the world of computing, a symbolic link is a term that is often referred to as the methodology where the reference of a file or a directory is contained in another file either through an absolute or relative path. In the year 1978, this term was often referred to in the DEC and RDOS mini computers. In today’s world, this methodology is widely used as a standard in POSIX operating systems like Linux and limited support in Windows operating systems like Vista and Windows 7. Since windows are more famous, one might have had a glimpse of shortcut files in the Windows operating system, which gives a sense of symbolic link. In this topic, we are going to learn about Linux Symbolic Link.
Now the next thing to understand is the syntax. Symbolic linking in Linux is achieved by the ln command. ln command is a utility made possible through the command line in Linux, and by default, they are used for creating hard links. We will talk about different types of linking in our next section, as over here we will talk about syntax, which goes as:
ln -s [OPTIONS] FILE LINK
Here, [OPTIONS] is an optional parameter and works only with using FILE and LINK. Also, LINK is the symbolic link to which FILE is linked to. This FILE can either be a file or even a directory. In case a directory replaces FILE, LINK should also be a directory.
How does Symbolic Link work in Linux?
Before we start off to understand how symbolic Link works in Linux, we would first understand the different types of links possible in Linux. There are 2 types of Links possible:
- Hard Links: Hard links are like an alternate or additional name to a file. With the help of the same index node, 2 or more files are associated. This option, although, is only available for either filesystems or partitions and is invalid for directories.
- Soft Links or also known as symbolic links: Soft links are analogous to shortcuts in Windows. If one doesn’t know about this feature, shortcuts are nothing but files that contain the location of the actual file or directory, and what it does is pointing to the actual location in case the link is invoked.
It is now well time to understand the working of a symbolic link, and once we know the working, it will be time for us to look at the actual working through some examples along with different options available for a symbolic link.
As we have already known, a symbolic link is a file whose content is the file’s actual location or directory. It keeps a flag in the directory entry to indicate that it is a symbolic link, more commonly known as a symlink. Now, as this symlink is opened, the operating system will read the content of the file, which denotes the actual location of the file and follow to the location and find the target file. Now, in some cases, when the file in the location is also a symlink, the same procedure is followed until the final file where it arrives is not a symlink. The operating system then obtains the index node (inode) of the final file where the routine has reached. This inode contains all details of the file like metadata, modification times, a pointer to the data of the file, and much other such information. This inode is now opened and, from now on, is used for reading and writing to the file.
In most cases, obviously with some exceptions, any operations on file data on the symlink will automatically get reflected into the final file to which it is referring. In contrast, any metadata related operations, for example, renaming or deleting, will affect the symlink. A small representation below can sum up the entire discussion we had in this paragraph.
Below are a few pointers one should be aware of when a symlink is used in the Linus operating system.
- Deletion of the symlink is possible even without affecting the file where the data is present actually. The reasoning behind this is quite simple, the inode where the link of the data is present is the inode of the actual file, and that differs from the inode of the symlink. Hence, even if the symlink is deleted, only the connection is lost to reach the final file and not the actual file.
- With symlinks, it is possible to span across the filesystem.
- As we discussed already that linking both files and directories is possible using the symlink
- One can easily figure out if a file is a symlink by looking at the presence of characters before the place where all the permission related information is present.
1. Creating a symlink
ln -s fileForLink.txt symlinkForPractice ls
2. Verifying a symlink
ls -l fileForLink.txt symlinkForPractice
Here we can look for 2 main pointers:
- Presence of character lat the start ofsymlinkForPractice
- The linking of the file from symlink to the location of the destination file. This destination file is the destination of the link and not the final file.
3. Removing a symlink
rm symlinkForPractice ls -l fileForLink.txt symlinkForPractice
One important thing one must observe is that symlinkForPractice is no more a link that is present in the memory altogether, and hence when we command the Linux to fetch the link, we end up with an error. Now in case we have another link which points to the link which is removed, and we would like to observe the effect of the output, we would see that some highlight will be present informing the developer about some problem with the connection as the destination of the other link which is nothing but the link which is deleted is unavailable. Below is a snapshot that mentions the same.
ls -l fileForLink.txt symLinkToLink
In this article, we have taken a full round of what is in store in the symbolic link and what are the other elements associated with the same. It is often preferred to use a symlink instead of a hard link as symlinks have the capability to include a directory as well, which hard links don’t, and the main reason is in the hard link if directory linking is allowed, it will tend to break the file system structure.
We hope that this EDUCBA information on “Linux Symbolic Link” was beneficial to you. You can view EDUCBA’s recommended articles for more information.