This guide explains how to create and delete symbolic links in Linux systems like Ubuntu and Red Hat. A symbolic link points to another file, saving disk space since it avoids duplicating data.
Table of Contents
What Is a Symbolic Link?
A symbolic link, or symlink, is a Linux file that points to another file or directory. Unlike regular files, symlinks store the path to the target. This is similar to shortcuts in Windows. Symlinks can point to targets on the same or different filesystems, making them highly useful for organizing files.
To create a symlink, use the ln
command with the -s
option:
ln -s target_path symlink_name
Here, target_path
is the file or directory to link, and symlink_name
is the new link.
Keep in mind:
- Symlinks don’t store actual data—only the target’s path.
- If the target moves or is deleted, the symlink breaks and won’t work.
Despite potential breakage, symlinks are great for flexible file management.
Benefits of Symbolic Links
- Efficiency: Access files without duplicating them. Organize logically without altering actual locations.
- Save Space: Symlinks use minimal disk space since they only store a path.
- Cross-Filesystem Flexibility: Unlike hard links, symlinks work across partitions or network filesystems.
- Simpler Navigation: Create easy paths to deeply nested files or directories.
- Version Control: Link to the latest version of a file without updating paths everywhere.
- Integration: Applications and scripts use symlinks to simplify dynamic file management.
Creating a Symbolic Link
To create a symlink, use this command:
ln -s [target_path] [symlink_path]
For example, to link /logs/mongrel.log
to /usr/local/bin/mongrel/logs/mongrel.log
:
ln -s /usr/local/bin/mongrel/logs/mongrel.log /logs/mongrel.log
This lets you access the log file from /logs
instead of its long original path.
Note: Ensure the /logs
folder exists before creating the symlink.
Removing a Symbolic Link
Remove a symlink with the rm
command:
rm [symlink_path]
This only removes the link, not the target file.
Handling Broken Symlinks
A symlink becomes “broken” if its target is moved or deleted. To find broken symlinks, run:
find /path/to/directory -xtype l
This lists all invalid symlinks in the specified directory.
To delete a broken symlink, use:
rm /path/to/broken/symlink
or
unlink /path/to/broken/symlink
The unlink
command is safer, as it only works on symlinks.
Cleaning up broken symlinks keeps your system tidy and avoids errors caused by missing files.
FAQs
Can I update a symlink?
Yes, you can update a symlink by recreating it. First, remove the existing symlink using the rm
or unlink
command, then create a new symlink with the ln -s
command pointing to the new target. This process effectively updates the symlink to point to a different file or directory.
What happens if the target file of a symlink is moved or deleted?
If the target file of a symlink is moved or deleted, the symlink becomes broken, meaning it points to a non-existent location. The symlink itself remains, but accessing it will result in an error until the target is restored or the symlink is updated to point to a new, existing target.
How do symlinks work with backup and version control systems?
Symlinks are typically treated as links by backup and version control systems, meaning the system stores the path information of the symlink rather than the content of the file or directory it points to. This ensures that the symlink structure is preserved when data is restored or checked out from version control.
use unlink command for remove symbolic link
# unlink then link name
Thanks for providing the useful article on Removing symbolic links in linux
Thank you for the useful info published on this article…