If you’ve used any operating system, you are familiar with links or files pointing to the location of other files or directories.
In Linux, these files are known as Symbolic Links or SymLink for short. These links direct users to a specific file or directory on the local system or a linked file system on a different system. If you’re coming from a Windows background, you know symbolic links in Linux as shortcuts.
This article covers the basics of symbolic links in Linux, how you can create and remove them, and the important things you need to know when working with these links.
Let’s start with the types of symbolic links.
Types of Symbolic Links in Linux
Symbolic links in Linux come in the following two categories.
Soft links are files or links that point to other files and directories located on the local system and other linked file systems. Essentially, these links act as hyperlinks to the file system on a Linux filesystem. Note that these links point to files and do not contain the contents of the files.
When working with soft links, you should remember that:
- Soft links cease to work if the original file gets deleted (or becomes unavailable for any reason).
- Soft links can point to files and directories located on other linked file systems.
- Deleting a soft link doesn’t ensure the deletion of the target file.
A hard link also refers to a file on the local system. However, unlike soft links, these links often mirror information about the target file. This means a hard link essentially makes a copy of the file so that even if the target files become unavailable, you can still access the contents through the hard link. When working with hard links, remember:
- Hard links only work with files and cannot point to directories.
- Hard links still work if the target file is relocated
- Hard links can be created or referred to in the same file systems.
Now that you clearly understand symbolic links in Linux, it is time to dive into the technicalities of creating and removing symbolic links.
Working With Symbolic Links in Linux
When working with symbolic links, it’s important to note that they can be substituted for target files in commands. This means if you have a symbolic link, you don’t have to worry about referring to the actual location of the file.
How to Create a Symbolic Link in Linux
Linux offers the link (ln) command for creating symbolic links. The typical syntax of the command is:
ln -s <path to the target file or directory> <the path of the symbolic link>
Note that without the -s flag, the ln command creates hard links (the command’s default behaviour).
How to Create a Symbolic Link to a File
You must provide the path to the target file to the ln command.
# ln -s /home/imp/RS_test.txt rs.txt
After the successful execution of the command, you can use rs.txt to refer to the original file,
How to Create a Symbolic Link to a Directory
To create a symbolic link to a folder, you can use the ln command with the -s flag.
#ln -s test/newRSdir/ newRSdir
Now you can use newRSdir to refer to the folder /test/newRSdir/.
How to Remove a Symbolic Link in Linux
Deleting the symbolic link is a simple method of decoupling the target file and the name of the symbolic link so that Linux no longer sees the symbolic link pointing to the target file. For this, we’ll use the unlink command.
# unlink <path-to-symbolic link>
Now that the symbolic link has been unlinked, we can safely remove it with the rm command, just like any other file on the system.
# rm <path-to-symbolic link>
Important Things To Remember About Symbolic Links
You know what symbolic links are and how you can create and remove them from the system. Now it’s time to discuss some essential points you need to know when working with these links.
Changes In The Link Are Reflected In The Target
Since you can access the target file via the symbolic link, any changes you make to the link file will be reflected in the original. For instance, anytime you access the link file, the original file’s timestamp is updated to show the latest access time through the link.
You Need To Follow The Link To Find Out Where It Leads
Since the name of the symbolic link doesn’t give any indication, it’s very difficult to guess whether the target is a file or a directory. We highly recommend checking the target before using the link to avoid confusion and errors.
Links Have Full 777 Permission (But That Doesn’t Matter!)
Note that access to a file works based on the permission flags of the file. This means that even if a symbolic link has 777 permission (any user can access the link), your ability to view and modify the target’s contents depends upon the target’s permission. So if you don’t have direct access to a target file, you can’t use the symbolic link to access the file.
You Can Daisy-Chain Symbolic Links
While you can point a link to a link to a link to a file, we highly recommend you avoid this chained symbolic link. It could quickly get confusing, and a broken link in the chain could throw up unanticipated errors.
Symbolic links, found in Linux and UNIX systems, provide a convenient way to access files or folders that are difficult to reach. With practice, one can develop an intuitive understanding of how they work, leading to improved efficiency in managing file systems.