Skip to content

Symbolic (Soft) Links in Linux — Creating Flexible File References

A Symbolic Link (Soft Link or Symlink) is a special type of file that points to another file or directory by its pathname. Unlike a hard link, a symbolic link has its own inode and acts like a shortcut. Symbolic links are widely used in Linux for application deployments, version management, configuration sharing, and storage management.


Learning Path

Linux Mastery → Module 4: File Management and Permissions → Lesson 3

Difficulty: Beginner → Intermediate

Reading Time: 40 Minutes

Course Progress

Course: Linux Mastery

Module: File Management and Permissions

Lesson: 3 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand symbolic links
  • Create soft links
  • Understand relative and absolute symlinks
  • Identify broken symbolic links
  • Compare hard links and symbolic links
  • Manage symlinks in production
  • Troubleshoot common symlink issues

Prerequisites

Before starting this lesson, complete:

  • Module 1 – Linux Fundamentals
  • Module 2 – Command Line Essentials
  • Module 3 – Text Processing
  • Module 4 Lessons 1–2

Why Learn Symbolic Links?

Suppose your application is deployed here:

/opt/myapp/releases/v1.0/

Later you deploy:

/opt/myapp/releases/v2.0/

Instead of changing every script, update a single symbolic link:

/opt/myapp/current

Now every service automatically uses the latest version.

This is exactly how many production deployments work.


What is a Symbolic Link?

A symbolic link is a special file that stores the path to another file or directory.

It behaves like a shortcut.

Syntax:

ln -s TARGET LINK_NAME

Linux View

latest
notes.txt
Actual File Data

Unlike a hard link, the symbolic link points to the pathname, not the inode.


Create a Symbolic Link

Create a file.

echo "Linux Mastery" > notes.txt

Create a symbolic link.

ln -s notes.txt latest.txt

Display:

ls -l

Output:

lrwxrwxrwx 1 basha users 9 Jan 10 latest.txt -> notes.txt

Notice:

  • Starts with l
  • Shows the target using ->

Read Using the Link

cat latest.txt

Output:

Linux Mastery

The symbolic link behaves like the original file.


Modify Through the Link

Append data.

echo "Docker" >> latest.txt

Display original.

cat notes.txt

Output:

Linux Mastery

Docker

View Link Details

ls -l latest.txt

Output:

latest.txt -> notes.txt

Display inode information.

ls -li

Example:

524288 notes.txt

524450 latest.txt

Notice:

  • Different inode numbers
  • Different file types
  • The symbolic link stores only the target path

Delete the Original File

rm notes.txt

Now:

cat latest.txt

Output:

cat: latest.txt: No such file or directory

The symbolic link still exists, but its target is gone.

This is called a broken symbolic link.


Broken Symbolic Link

View:

ls -l

Output:

latest.txt -> notes.txt

Although the link exists, it no longer points to a valid file.

Find broken symbolic links.

find . -xtype l

Remove a Symbolic Link

Delete the link.

rm latest.txt

or

unlink latest.txt

Only the link is removed.

The original file (if it exists) is unaffected.


Symbolic Links to Directories

Create:

mkdir projects

Create a symbolic link.

ln -s projects workspace

Display.

ls -l

Output:

workspace -> projects

Navigate:

cd workspace

This works exactly like entering the original directory.


Absolute vs Relative Symbolic Links

Absolute Path

ln -s /home/basha/docs/report.txt report-link

Stores:

/home/basha/docs/report.txt

Relative Path

ln -s ../docs/report.txt report-link

Stores:

../docs/report.txt

Which One Should You Use?

Type Best For
Absolute Fixed system paths
Relative Portable projects and repositories

Many software projects prefer relative symbolic links because they continue working if the project directory is moved.


Hard Link vs Symbolic Link

Hard Link Symbolic Link
Same inode Different inode
Points to file data Points to pathname
Cannot span filesystems Can span filesystems
Usually cannot link directories Can link directories
Continues working if original filename is removed Becomes broken if target is removed

View Link Target

readlink latest.txt

Output:

notes.txt

Display the absolute target.

readlink -f latest.txt

Find All Symbolic Links

find . -type l

Find broken symbolic links.

find . -xtype l

Common Commands

Create:

ln -s source target

View:

ls -l

Read target.

readlink target

Delete.

unlink target

Real Production Examples

Current application release.

/opt/app/current -> releases/v2.4

NGINX configuration.

sites-enabled -> sites-available

Java installation.

/usr/bin/java -> /etc/alternatives/java

Python versions.

python -> python3

Shared storage.

logs -> /mnt/storage/logs

Production Perspective

Symbolic links are heavily used in:

  • Kubernetes deployments
  • NGINX configuration
  • Apache virtual hosts
  • Docker volumes
  • Java alternatives
  • Python virtual environments
  • Blue-Green deployments
  • Rolling application upgrades

They allow applications to switch between versions without changing configuration files.


Hands-on Lab

Task 1

Create a file.

echo "Linux" > linux.txt

Task 2

Create a symbolic link.

ln -s linux.txt latest.txt

Task 3

Display.

ls -l

Task 4

Read the file through the symbolic link.

cat latest.txt

Task 5

Append data.

echo "Docker" >> latest.txt

Task 6

Delete the original file.

rm linux.txt

Try reading the symbolic link.


Task 7

Find broken symbolic links.

find . -xtype l

Task 8

Remove the symbolic link.

unlink latest.txt

Command Deep Dive

Command Purpose Production Example
ln -s Create symbolic link Deployments
ls -l Display link target Troubleshooting
readlink Show target Verification
unlink Remove link Cleanup
find -type l Find symlinks Audits
find -xtype l Find broken links Maintenance

Production Troubleshooting Scenario

Scenario

An application suddenly fails after a deployment.

Investigation:

  1. Verify the current symbolic link.
  2. Check if the target directory exists.
  3. Confirm the application points to the correct release.
  4. Update the symbolic link if necessary.

Commands:

ls -l /opt/myapp/current

readlink /opt/myapp/current

find /opt/myapp -xtype l

A broken symbolic link is discovered after the old release directory was accidentally removed.

The fix:

ln -sfn /opt/myapp/releases/v2.0 /opt/myapp/current

The -n and -f options replace the existing link without affecting the target directory.


Best Practices

  • Prefer symbolic links when linking directories.
  • Use relative symbolic links inside projects for portability.
  • Verify links after deployments.
  • Remove broken symbolic links during maintenance.
  • Use meaningful link names such as current, latest, or active.

Common Mistakes

❌ Deleting the target instead of the symbolic link.

✅ Always verify with:

ls -l

before deleting files.


❌ Assuming a symbolic link stores the file itself.

✅ It stores only the target path.


❌ Using absolute symbolic links in portable projects.

✅ Relative links are often more resilient when moving directories.


Interview Questions

Beginner

  1. What is a symbolic link?
  2. Which command creates a symbolic link?
  3. How do you identify a symbolic link using ls -l?
  4. What happens if the target file is deleted?

Intermediate

  1. Explain the difference between hard and symbolic links.
  2. What is a broken symbolic link?
  3. How do you find all symbolic links?
  4. How do you display the target of a symbolic link?

Architect Level

  1. Why are symbolic links commonly used in application deployments?
  2. How do symbolic links support Blue-Green deployments?
  3. When would you use a relative symbolic link instead of an absolute one?

Summary

In this lesson, you learned:

  • Creating symbolic links
  • Reading and modifying files through symbolic links
  • Relative vs absolute symbolic links
  • Broken symbolic links
  • Viewing link targets
  • Production deployment use cases

Symbolic links provide flexible references to files and directories. They are widely used in Linux system administration, DevOps, and production environments to simplify configuration management and application deployments.


Key Takeaways

  • A symbolic link is a special file that stores a pathname.
  • Use ln -s to create symbolic links.
  • Symbolic links have their own inode.
  • If the target is removed, the symbolic link becomes broken.
  • Symbolic links can point to directories and span different filesystems.
  • They are commonly used for version management and deployment strategies.

What's Next?

Linux File Permissions — Understanding Read, Write, and Execute

In the next lesson, you'll learn:

  • Reading permission strings
  • User, Group, and Others
  • Read, Write, and Execute permissions
  • Numeric (octal) permission modes
  • Directory permissions and production security patterns