Symbolic links in Linux are a powerful feature of the Linux file system that allow you to create a shortcut to a file or directory. They are similar to shortcuts in Windows, but much more flexible and powerful.
In this article, we’ll take a look at what symbolic links are, how they work, and how you can use them to make your life easier.
What are symbolic links?
In Linux, symbolic links (also known as symlinks or soft links) are a file type that points to another file or directory. They are frequently used for creating shortcuts or to access files or directories on different filesystems.
Symbolic links differ from hard links in that they do not contain the data of the file or location to which they point. Instead, they contain a reference to the file or directory’s location. This means that symbolic links can be used to access files or folders that have been relocated or renamed without the need for an update.
Why use symbolic links?
Symbolic links in Linux can be useful for various reasons, such as:
Organizing your files: You can create symbolic links in Linux to group related files or directories in different locations without having to move or copy them.
Sharing your files: You can create symbolic links in Linux to share files or directories with other users or applications without having to change their permissions or ownership.
Saving disk space: You can create symbolic links in Linux to avoid duplicating large files or directories that are used by multiple programs or users.
Simplifying complex directory structures: You can create symbolic links in Linux to shorten long or confusing paths that are hard to remember or type.
How to create symbolic links with the ln command?
To create a symbolic link with the ln command, you need to open a terminal window and type the following syntax:
ln [option] /path/to/the/file file
The -s option tells the ln command to create a soft link. If you omit this option, it will create a hard link by default. The target is the file or directory that you want to link to, and the link is the name and location of the symbolic link that you want to create. You can specify either absolute or relative paths for both the target and the link.
Here’s how to create a symbolic on Linux with the ln command:
Launch Terminal.Navigate to the directory containing the file or directory you want to link to.Enter the following code:
ln -s [source] [destination]
The path of the file or directory to which you want to link is specified as source.The name of the symbolic link you want to create is destination.
To create a symbolic link to the file example1.txt in the current directory, for example, use the following command:
ln -s example1.txt example2.txt
To create a symbolic link to the directory /files and name it as folder, use the command:
cloudbooklet@ubuntu: ln -s /home/folder cloudbooklet@ubuntu: ls folder
Once you’ve typed the command, press Enter. If the command was successful, a new file or directory named folder will be created in the current directory.
How to Replace Symbolic Links in Linux?
In Linux, use the -f (force) option with the ln command to rewrite a symbolic link. The existing symbolic link will be overwritten, even if it points to a different file.
Here’s the syntax to overwrite Symbolic Links:
ln -sf old_link new_link
For example, to replace a symbolic link named old_link.txt with a new file named new_link.txt, use the command:
ln -f example1.txt example2.txt
The existing symbolic link example1.txt will be replaced with a new symbolic link pointing to the file example2.txt.
cloudbooklet@ubuntu: ~/folder$ ls example1.txt cloudbooklet@ubuntu: ~/folder$ ln -s example1.txt example2.txt cloudbooklet@ubuntu: ~/folder$ ls example1.txt example2.txt cloudbooklet@ubuntu: ~/folder$ ln -s example1.txt example2.txt ln: failed to create symbolic link ‘example2.txt’: File exists cloudbooklet@ubuntu: ~/folders$ ln -sf example1.txt example2.txt cloudbooklet@ubuntu: ~/folder$ ls example1.txt example2.txt cloudbooklet@ubuntu: ~/folder$
If the symbolic link already exists and you do not use the -f option, the ln command will fail. This is due to the ln command’s inability to overwrite an existing file or directory.
You can also check out our blog, How to Upgrade Linux Kernel in Ubuntu for more tips and tutorials for How to upgrade Linux kernel.
How to Find and Remove symbolic links with the rm command?
The following are the steps for find and removing broken symbolic links in Linux:
Using the find command, locate the broken symbolic links. The find command can be used to look for files and directories that match a set of criteria. To find broken symbolic connections, use the following syntax:
rm example1.txt
If you are not in the same directory as the broken symbolic link, you can use the -path option to indicate the path to the directory. For example, to locate a broken symbolic link you don’t need to mention the path directory /hohome/directory_name, run the command:
find ~/directory_name -xtype l
Finally, you can use the following command to get rid the broken symbolic link:
find ~/directory_name -xtype l -delete cloudbooklet@ubuntu: ~/folder$ ls example1.txt example2.txt cloudbooklet@ubuntu: ~/folder$ rm example1.txt cloudbooklet@ubuntu: ~/folders$ find -xtype l ./example2.txt cloudbooklet@ubuntu: ~/folders$ find -xtype l -delete cloudbooklet@ubuntu: ~/folder$ ls cloudbooklet@ubuntu: ~/folder$
How to remove symbolic links with the rm command?
To delete a symbolic link in Linux with the rm command, use the following syntax:
rm [symbolic]
To remove a symbolic link in Linux to a directory, use the rm -rf command. It is crucial to note, however, that this will also remove the contents of the directory.
rm -rf “folder” cloudbooklet@ubuntu: ~/folder$ ls example1.txt example2.txt cloudbooklet@ubuntu: ~/folders$ rm example2.txt cloudbooklet@ubuntu: ~/folder$ ls example1.txt cloudbooklet@ubuntu: ~/folder$
Deleting a symbolic link does not affect or delete its target file or directory. It only removes the reference to it.
You can use the unlink command to remove only the symbolic link and not the contents of the directory. To remove a symbolic link named link that leads to a directory named mydir, for example, run the command:
unlink /path/to/the/link
This deletes the symbolic link link without removing the directory mydir’s contents.
Conclusion
In this article, you learned what are symbolic links in Linux, why they are useful, how to create and delete them, and how to use them in Linux. Symbolic links in Linux are special files that point to other files or directories on your system. You can create and delete symbolic links in Linux with the ln command or with a graphical tool.
If you find this article helpful, please leave us a comment. Thank you very much for your time.
Symbolic Links in Linux: What They Are and How to Use Them