Fundamentals 6 min read

Master Linux Symbolic Links: Types, Creation, Deletion, and a Bash Script to List Targets

This guide explains Linux symbolic (soft) links, distinguishes hard and soft links, shows how to create and remove them with ln and unlink, demonstrates retrieving a link's target using readlink, and provides a Bash script that enumerates all symlinks in a directory and displays their destinations.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Symbolic Links: Types, Creation, Deletion, and a Bash Script to List Targets

What is a Symbolic Link?

A symbolic (soft) link is a special file that points to another file or directory anywhere in the filesystem, acting as a shortcut.

Types of Links

Hard link : Direct pointer to a file's inode; can only be created within the same filesystem.

Soft link (symbolic link): An indirect reference that can reside on any filesystem and can point to files on different filesystems.

Creating Symbolic Links

The ln command creates links. Without options it creates a hard link; to create a soft link, add the -s flag.

ln [OPTIONS]
ln -s [OPTION]

Example – create a soft link to a file:

[root@localhost ~]# ln -s /var/log/audit/audit.log ~/audit.log

Example – create a soft link to a directory:

[root@localhost ~]# ln -s /var/log ~/log

Removing Symbolic Links

Use the unlink command followed by the link path.

[root@localhost ~]# unlink ~/log

Getting the Target of a Symbolic Link

The readlink command prints the path that a symbolic link points to.

[root@localhost ~]# readlink ~/audit.log
/var/log/audit/audit.log

Bash Script to List All Symbolic Links and Their Targets

The script prompts for a directory, scans it for symlinks, and displays each link with its resolved target.

#!/bin/bash
read -p "Provide the directory to evaluate: " target_dir
cd $target_dir
links=$(find . -maxdepth 1 -type l -ls | awk '{print $11}')
for link in $links
do
    echo "$link -> $(readlink $link)"
done

Sample output when evaluating /etc:

[root@localhost ~]# ./symlink.sh
Provide the directory to evaluate: /etc
./rc.local -> rc.d/rc.local
./redhat-release -> centos-release
./system-release -> centos-release
./init.d -> rc.d/init.d
./rc0.d -> rc.d/rc0.d
... (additional entries) ...

Conclusion

The article covered the basics of Linux symbolic links, demonstrated how to create and delete them, showed how to retrieve their targets, and provided a reusable Bash script for enumerating all symlinks in a given directory.

Symbolic link illustration
Symbolic link illustration
Bash script example
Bash script example
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

scriptreadlinkSymbolic Linkln
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.