Skip to content

How to Fix Linux Broken Symlink Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Linux Broken Symlink Error. We cover key concepts, practical examples, and best practices.

You try to access a symlink and get No such file or directory — the file the symlink points to has been moved or deleted, leaving a dangling reference.

The Problem

ls -la link.txt
lrwxrwxrwx 1 user user 24 Jun 24 10:00 link.txt -> /path/to/nonexistent/file.txt

cat link.txt
cat: link.txt: No such file or directory

Step-by-Step Fix

find . -type l ! -exec test -e {} \; -print

Or more efficiently:

find . -xtype l
# Remove all broken symlinks in current directory
find . -xtype l -delete

# Remove with confirmation
find . -xtype l -ok rm {} \;
# WRONG — points to moved/deleted file
ln -s /old/path/file.txt link.txt

# RIGHT — point to the new location
rm link.txt
ln -s /new/path/file.txt link.txt
ls -la link.txt

Expected:

lrwxrwxrwx 1 user user 24 Jun 24 10:01 link.txt -> /new/path/file.txt

cat link.txt
file contents here
realpath link.txt

If the target does not exist, you get:

realpath: link.txt: No such file or directory
# WRONG — target relative to current directory, not symlink location
ln -s ~/project/data/file.txt symlink_in_other_dir

# RIGHT — use relative path from symlink's directory
rm symlink_in_other_dir
ln -s ../data/file.txt symlink_in_other_dir

Prevention Tips

  • Use ln -sr for relative symlinks
  • Check symlink targets after moving files
  • Use find . -xtype l periodically to audit
  • Update symlinks when restructuring directories
  • Prefer hard links for files that must stay in sync
  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between a hard link and a symlink?

A hard link is a direct directory entry pointing to the same inode as the original file — deleting the original does not affect the hard link. A symlink is a special file that stores a path to another file — if the target is deleted, the symlink breaks.

Use ln -sf new_target link_name. The -f flag removes the existing symlink before creating the new one. This is atomic for the recreation.

Yes, but chained symlinks can be confusing. Avoid it. Use realpath to resolve the full chain and point directly to the final target.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro