Fundamentals 8 min read

Master Linux Hard and Symbolic Links: When and How to Use ln

This guide explains the concepts, differences, and practical commands for creating hard links and symbolic (soft) links in Linux, illustrating their behavior with examples, showing their advantages, limitations, and how link counts change across files and directories.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Hard and Symbolic Links: When and How to Use ln

Hard Link (硬链接)

In Linux each file has an inode that stores its data blocks. The directory maps filenames to inode numbers, so multiple filenames can point to the same inode, creating a hard link. A hard link adds a new directory entry that references an existing inode.

$ ll -i /etc/crontab
131213 -rw-r--r-- 1 root root 722 Nov 16 2017 /etc/crontab

Creating a hard link for /etc/crontab in the current directory:

$ 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/crontab

The inode numbers are identical, confirming they refer to the same file. The third column (link count) increased from 1 to 2, indicating two filenames link to the same inode.

Characteristics of hard links

Consume almost no additional disk space.

The file is not truly deleted until all hard links are removed.

Limitations of hard links

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. $ sudo ln -s /etc/crontab crontab2 Listing the original and the symbolic link shows different inodes, different permissions, and the file type marked as "l" for link. The link count of the target file remains unchanged, and the size of the symbolic link file equals the length of the stored path (12 bytes).

Symbolic links can point to directories and can cross filesystem boundaries: $ ln -s /bin testbin Accessing testbin behaves exactly like accessing /bin.

Deleting the source file

When the target of a symbolic link is removed, the link becomes dangling and any operation on it yields “No such file or directory”.

$ echo "hello world" > hello.txt
$ ln -s hello.txt hello2
$ cat hello2
hello world
$ rm hello.txt
$ cat hello2
cat: hello2: No such file or directory

File Link Count

Creating a hard link increments the link count of the 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 its entry in the parent directory. Creating a new directory adds a hard link to its parent.

$ mkdir /home/nick/testdir
$ ll /home/nick | grep testdir
drwxr-xr-x 2 nick nick 4096 Jul 10 08:58 testdir/

The new empty directory has two hard links: /home/nick/testdir and

/home/nick/testdir/.

ln Command

The ln command creates links between files:

ln source target        # hard link
ln -s source target    # symbolic (soft) link

The -f option forces overwriting an existing target.

References: ln man page, 鸟哥的私房菜, 彻底理解“软链接”和“硬链接”, The difference between hard and soft links
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.

LinuxUnixfile systemHard LinkSymbolic Linkln command
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.