How to create/remove symlinks (symbolic link) in Linux

rpm dependencies, linux, ubuntu

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.

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

  1. Efficiency: Access files without duplicating them. Organize logically without altering actual locations.
  2. Save Space: Symlinks use minimal disk space since they only store a path.
  3. Cross-Filesystem Flexibility: Unlike hard links, symlinks work across partitions or network filesystems.
  4. Simpler Navigation: Create easy paths to deeply nested files or directories.
  5. Version Control: Link to the latest version of a file without updating paths everywhere.
  6. Integration: Applications and scripts use symlinks to simplify dynamic file management.
See also  Build an RPM in CentOS, RHEL and Ubuntu Linux

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.

3 thoughts on “How to create/remove symlinks (symbolic link) in Linux”

Leave a Comment