Mastering Linux Links: Soft, Hard, and Symbolic Link Commands Explained
This guide explains what file links are in Linux, distinguishes soft, hard, and symbolic links, and provides step‑by‑step command‑line examples for creating, inspecting, and removing each type of link, including handling directories and inode behavior.
What Is a Link?
A link creates a reference between files in a Unix/Linux filesystem. The ln command is used to create links, while ln -s creates symbolic (soft) links. The manual page man ln describes the basic usage without differentiating link types.
Soft (Symbolic) Link
A soft link is a special file that points to another file by pathname. The new file’s inode contains the path to the target, so the inode numbers differ. Permissions show an l flag (e.g., lrwxrwxrwx). Deleting the target leaves a broken link.
shashi@linuxtechi ~}$ touch src.txt
shashi@linuxtechi ~}$ cat > src.txt
Hello World
^C
shashi@linuxtechi ~}$ ln -s src.txt dst.txt
shashi@linuxtechi ~}$ ls -l
lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
shashi@linuxtechi ~}$ cat dst.txt
Hello WorldHard Link
A hard link creates an additional directory entry that points to the same inode as the original file. Both files share the same inode number, so changes to one affect the other. Hard links cannot span filesystems or directories.
shashi@linuxtechi ~}$ touch 123.txt
shashi@linuxtechi ~}$ ln 123.txt 321.txt
shashi@linuxtechi ~}$ ls -l
-rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt
-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
shashi@linuxtechi ~}$ ls -li
794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt
794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txtIf the original file is removed, the hard‑linked file remains because the inode still has a reference count.
shashi@linuxtechi ~}$ rm 123.txt
shashi@linuxtechi ~}$ ls -l
-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txtHard links cannot be created for directories; attempting to do so yields an error.
shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln abc def
ln: abc: hard link not allowed for directoryCreating Links in Practice
The guide walks through a full workflow: creating a source file, populating it, making a soft link, verifying inode numbers with ls -li, and observing that modifications to the source are reflected in the link.
shashi@linuxtechi ~}$ touch src.txt
shashi@linuxtechi ~}$ echo "Hello World" > src.txt
shashi@linuxtechi ~}$ ln -s src.txt dst.txt
shashi@linuxtechi ~}$ ls -li
794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txtSymbolic links can also point to directories, allowing files created in the linked directory to appear in the target directory.
shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln -s abc def
shashi@linuxtechi ~}$ cd abc
shashi@linuxtechi ~}$ touch 123.txt
shashi@linuxtechi ~}$ cd ..
shashi@linuxtechi ~}$ ls -l
drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc
lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abcRemoving Links
Soft links are removed with rm or unlink. Removing the link does not affect the target file.
# rm dst.txt
# unlink dst.txtDirectories that are symbolic links are removed the same way.
# rm def
# unlink defSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
