Master Linux Hard and Symbolic Links: When and How to Use ln
This guide explains the differences between hard and symbolic links in Linux, demonstrates how to create and inspect them with the ln command, and covers link counts, limitations, and practical examples for managing files and directories.
Hard Link (硬链接)
In Linux each file has an inode that points to data blocks. The directory stores the filename and its inode number. Multiple filenames can point to the same inode, creating a hard link. The link adds another directory entry referencing the same inode.
Example using ll -i /etc/crontab shows inode 131213. Creating a hard link with sudo ln /etc/crontab . produces a new name crontab that shares the same inode and link count increases from 1 to 2.
$ ll -i /etc/crontab
131213 -rw-r--r-- 1 root root 722 Nov 16 2017 /etc/crontab
$ sudo ln /etc/crontab .
$ ll -i /etc/crontab crontab
131213 -rw-r--r-- 2 root root 722 Nov 16 2017 crontab
131213 -rw-r--r-- 2 root root 722 Nov 16 2017 /etc/crontabThe third column in the listing is the link count, indicating how many directory entries reference the inode.
Hard links occupy almost no extra space because they only add a directory entry.
They do not consume additional disk space.
The file is deleted only when the last hard link is removed.
Limitations:
Cannot span across different filesystems.
Cannot link directories.
Symbolic Link (软链接)
A symbolic link creates a new file with its own inode and data block that stores the path of the target file. The link file’s type is shown as l in listings. $ sudo ln -s /etc/crontab crontab2 Listing shows a different inode for the link, distinct permissions, and a size of 12 bytes (the length of the path string). The link count of the target file does not change.
Symbolic links can point to directories and can cross filesystem boundaries. $ ln -s /bin testbin Accessing testbin behaves like accessing /bin.
Link Count
Creating a hard link increments the link count of the target file; creating a symbolic link does not affect the target’s link count.
Directories have at least two hard links: one for the directory itself and one for the “.” entry. Creating a new directory adds a hard link in its parent directory, increasing the parent’s link count.
$ mkdir /home/nick/testdir
$ ll /home/nick | grep testdir
drwxr-xr-x 2 nick nick 4096 Jul 10 08:58 testdir/The empty directory has two hard links: /home/nick/testdir and /home/nick/testdir/. Its parent directory also gains an additional link via ...
ln Command Usage
The ln command creates links: ln source target – creates a hard link. ln -s source target – creates a symbolic link.
Option -f forces removal of an existing target before linking.
References
ln man page
鸟哥的私房菜
Understanding hard and soft links
The difference between hard and soft links
Signed-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.
