Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

Linux | Red Hat Certified | IT Technology | Operations Engineer

👇 Join the technical exchange QQ group with 1000 members, note [Official Account] for faster approval

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

1. Establishing Links

ln can be used to create links, but ln creates hard links by default. If you add the -s option, it creates a soft link (ln -s)

To create a soft link: ln -s source_file target_file (link name)

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

If the link points to a non-existent file, it will result in a dangling pointer

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

Executing a valid soft link works, but executing an invalid soft link does not work

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

ln creates a hard link which is just an alias for the file; they share the same inode

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

The kernel keeps track of the number of hard links; the inode 530047 has 2 hard links. A regular file has a hard link count of 1 because no other hard link points to it.

When we delete a file, we perform two actions: 1. Delete the corresponding record in the directory, 2. Decrease the hard link count by 1. If it reaches 0, the corresponding disk space is released.

To delete a link, you can use unlink or directly use rm (a link is also a file)

2. Soft Links

A soft link is akin to a shortcut, similar to a shortcut created on the desktop. When a soft link is established, it is a file with its own inode number. The data block of the soft link stores the address (path) of the file it points to. When you click this soft link, it executes the content of the file at that address. A soft link is an independent file; when a soft link is deleted, it does not affect the content of the target path. However, if the target file is deleted, the soft link becomes invalid and cannot find the source, just like a desktop shortcut that does not affect the application stored on the computer, but if the application is deleted, the soft link will fail to locate the corresponding software! Establishing a soft link makes it easier for users to find the absolute path of a file, allowing users to execute the same result simply by clicking the soft link!

3. Hard Links

A hard link is essentially giving a file an alias; a hard link is not an independent file and does not have an independent inode number. The actual file on disk is identified not by the filename, but by the inode. In Linux, multiple filenames can correspond to the same inode. Two hard links to a file are identical, just with different filenames. In the parent directory, the mapping count of inode to filename increases by 1. This also indirectly proves that the filename does not exist in the current directory; otherwise, the same inode would not have two filenames. A hard link adds a new filename and its corresponding inode mapping in the data block of a specific directory. Each inode can be pointed to by multiple filenames, and the number of hard links is usually counted using reference counting. Adding a hard link means copying the new filename and inode mapping in the directory. Thus, every file, whether a directory or a regular file, has an inode, and each inode has a reference count that tracks the number of hard links. The directory maintains the mapping relationship between filename and inode.

4. Application Scenarios for Soft and Hard Links

Soft link application scenario

It serves as a shortcut

There are numerous soft links in the system, view them using ls -/lib64/. -l

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

The most common application of soft links is creating shortcuts

Hard link application scenario

Creating a directory has a hard link count of 2, while creating a regular file has a hard link count of 1. Why?

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

Because every directory has two files: the file itself and the .. file

. represents the current directory, while .. represents the parent directory

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

Since . has the same inode number as the current directory, and .. has the same inode number as the parent directory, . is essentially an alias for the directory, which is a hard link. If a hard link is created in this directory, its hard link count would theoretically become 3. Every directory contains . and .. files to facilitate path switching. Linux systems do not allow hard links to directories to avoid circular reference issues that could lead to system bugs. Therefore, establishing hard links to directories is prohibited!

Hard links are commonly used for switching paths back and forth

Enhancing Your Linux File Management: A Comprehensive Guide to Hard and Soft Links

For course inquiries, add: HCIE666CCIE

↑ Or scan the QR code above ↑

What technical points and content do you want to see?

You can leave a message below to tell Xiaomeng!

Leave a Comment