Understanding Linux Hard and Soft Links: Commands, Differences, and Limits
This article explains the concepts of Linux hard and soft (symbolic) links, demonstrates how to create and inspect them with the ln command, and outlines their characteristics, advantages, and limitations compared to Windows shortcuts.
Introduction
In Windows, shortcuts are link files that become invalid when the target is removed. Linux uses links that are different: hard links and soft (symbolic) links.
Hard Links
Hard links are pointers to the original file's inode; they share the same inode and therefore the same file content.
Example:
[root@qll ~]# ll -i /etc/passwd
67544416 -rw-r--r--. 1 root root 882 Feb 5 11:50 /etc/passwdCreating a hard link:
[root@qll ~]# ln /etc/passwd passwd_test
[root@qll ~]# ll -i /etc/passwd passwd_test
67544416 -rw-r--r--. 2 root root 882 Feb 5 11:50 /etc/passwd
67544416 -rw-r--r--. 2 root root 882 Feb 5 11:50 passwd_testBoth entries have the same inode number, indicating they refer to the same file. The first character in the permission field is "-", meaning a regular file; the link count column increases from 1 to 2.
Characteristics and Limitations of Hard Links
Consume almost no extra space because only a directory entry is added.
The file is not truly deleted until all hard links are removed.
Cannot be created across different file systems.
Cannot link directories.
Soft (Symbolic) Links
Soft links store the pathname of the target, so they can point to directories and cross file‑system boundaries. When the target is removed, the symbolic link becomes dangling, similar to Windows shortcuts.
Example with /etc/passwd:
[root@qll ~]# ln -s /etc/passwd passwd_soft
[root@qll ~]# ll -i /etc/passwd passwd_soft
67544416 -rw-r--r--. 1 root root 882 Feb 5 11:50 /etc/passwd
100663362 lrwxrwxrwx. 1 root root 11 Feb 19 17:51 passwd_soft -> /etc/passwdThe inode numbers differ, showing they are separate files.
The permission field shows "l", indicating a symbolic link.
The link count remains 1, so creating a soft link does not increase the target’s link count.
The size of the symbolic link file equals the length of the pathname (11 bytes).
The last column displays the target path.
ln Command
1. Hard Link
[root@qll tmp]# ln /tmp/data.txt data2.txt # create hard link
[root@qll tmp]# rm -rf /tmp/data.txt # delete source file
[root@qll tmp]# cat data2.txt # still works because hard link remains2. Soft Link
[root@qll tmp]# ln -s /tmp/te.txt /tmp/te2.txt # create file soft link
[root@qll tmp]# ln -s /bin testbin # create directory soft link
[root@qll tmp]# rm -rf /tmp/te.txt # delete source file
[root@qll tmp]# cat /tmp/te2.txt # fails: No such file or directorySigned-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
