Home  >  Article  >  Operation and Maintenance  >  What is the difference between soft link and hard link

What is the difference between soft link and hard link

王林
王林forward
2020-08-03 17:32:535861browse

What is the difference between soft link and hard link

What is a hard link?

Hard link (hard link): A is a hard link to B (A and B are both file names), then the inode node number in A’s directory entry is the same as the inode number in B’s directory entry The node numbers are the same, that is, one inode node corresponds to two different file names, and the two file names point to the same file. A and B are completely equal to the file system.

(Recommended tutorial: linux tutorial)

If you delete one of them, it will have no effect on the other. Every time a file name is added, the number of links on the inode node increases by one. Every time a corresponding file name is deleted, the number of links on the inode node decreases by one until it reaches 0, and the inode node and the corresponding data block are recycled.

Note: Files and file names are different things. rm A deletes only the file name A, and the data block (file) corresponding to A will only be deleted when the number of inode node links is reduced to 0. System recycling.

What is a soft connection?

Soft link is a commonly used command in Linux. Its function is to establish a synchronous link for a certain file in another location.

(Video tutorial recommendation: linux video tutorial)

The specific usage is: ln -s source file target file.

When we need to use the same file in different directories, we do not need to put a file that must be the same in every required directory. We only need to use the ln command to link in other directories. (link) is fine, there is no need to repeatedly occupy disk space.

Difference:

1. Mount point

Only hard links can be created between files on the same storage media. , hard links cannot be created between files under different mount points. In the latter case, soft links can be used; (distinguish between different mount points and different directories on the same mount point)

2. Directory

Soft link is equivalent to a shortcut in win. That is, if a soft link to a directory is just a shortcut to a directory to a specified location, the operating system will directly find the file in the real directory when looking for this shortcut. But hard links are equivalent to mirroring. After creating a hard link to a directory, the operating system needs to make a hard link (copy a copy) of all the files in this directory, so that when the operating system accesses this link It needs to be traversed continuously, which greatly increases the complexity, and it is easy to enter an infinite loop.

Hard links cannot be created for directories due to the design of the file system. Directories in the Linux file system hide two special directories, the current directory and the parent directory. In fact, they are two hard links. If the system creates a hard link to the directory, a directory loop will occur.

The above is the detailed content of What is the difference between soft link and hard link. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete