A symbolic link also referred to as a soft link or symlink in Linux/Ubuntu is similar to shortcuts in Microsoft Windows. A user can utilize a symbolic link to create shortcuts, i.e., a Symbolic link is a file that points to a source file/directory.
This article will demonstrate the creation of a symbolic link on Ubuntu 22.04 LTS. This article will discuss:
- How to Create/Generate a Symbolic Link for a File in Linux/Ubuntu?
- How to Create/Generate a Symbolic Link for a Folder/Directory in Linux/Ubuntu?
- How to View a Symbolic Link’s Target in Linux/Ubuntu?
- How to Overwrite/Rewrite a Symbolic Link in Linux/Ubuntu?
- How to Remove/Delete a Symbolic Link in Linux/Ubuntu?
How to Create/Generate a Symbolic Link for a File in Linux/Ubuntu?
In this section, a symbolic link for a file will be created. The syntax for creating a symbolic link is:
Where,
- ln: command for creating symbolic link
- s: symbolic link flag to create a soft link.
Using the above syntax, a symbolic link for a file “5g.txt” can be created by executing the following command:
From the above terminal, it can be verified that the symbolic link is created successfully. Additionally, the creation of the symbolic links can be verified by using Ubuntu UI:
How to Create/Generate a Symbolic Link for a Folder/Directory in Linux/Ubuntu?
In this section, a symbolic link for a directory will be created. The syntax for creating a symbolic link is:
Where,
- ln: command for creating the symbolic link
- s: symbolic link flag to create a soft link.
Using the above syntax, a symbolic link for a dictionary “Others” can be created in the “Downloads” directory by executing the following command:
From the above image, it can be verified using the “ls” command in the Terminal that the symbolic link is created successfully in the “Downloads” directory. Additionally, the creation of a symbolic link can be verified by accessing the “Downloads” directory using Ubuntu UI:
How to View a Symbolic Link’s Target in Linux/Ubuntu?
The target, i.e., the file or the directory that the symbolic link is pointing to, can be viewed by using the “-l” flag along with the list(ls) command, which lists the files in long format. For example, the target of the “Others” symbolic link can be viewed by:
From the above image, it can be seen that the target of the “Others” directory is “/home/linuxuser/Documents/Others”.
How to Overwrite/Rewrite a Symbolic Link in Linux/Ubuntu?
In case, a symbolic link of a file or a directory exists and we need to overwrite a symbol link, using the usual command, i.e., “ln -sf 5g.txt 5glink” for overwriting a symbolic link for 5g.txt file will result in an error “ln: failed to create symbolic link ‘5glink’: File exists”. This error can be bypassed by adding the “-f” flag following the “-s” flag as shown below:
From the above image, it can be seen that the symbolic link “5glink” is successfully overwritten.
How to Remove/Delete a Symbolic Link in Linux/Ubuntu?
In case, we would like to remove the symbolic link because we don’t need it or if the source of the symbolic link is moved to another location, we can do that with either the “rm” or “unlink” command. To remove a symbolic link, we can run the following command:
From the image above, it can be verified that the symbolic link is removed. Alternatively, the following command is used to remove a symbolic link using unlink:
From the image above, it can be verified that the symbolic link is removed.
Conclusion
Symbolic links for files and directories are created by using the “ln” command with the “-s” flag where “-s” represents the symbolic link flag to create a soft link. This article has demonstrated how to create, view, overwrite, and delete a symbolic link on Ubuntu 22.04 LTS.