The difference between hard links and soft links in Linux

Source: Internet
Author: User

1. In principle:

Hard link: A is a hard link to B (both A and b are filenames), the Inode node number in A's directory entry is the same as the Inode node number in the directory entry for B, that is, an inode node corresponds to two different file names, two file names point to the same file, A and B are completely equal to the file system. If one is deleted, it has no effect on the other one. Each additional file name increases the number of links on the Inode node by one, each deleting a corresponding file name, and the number of links on the Inode node is reduced by one until the 0,inode node and the corresponding data block are recycled. Note: Files and filenames are different things, rm a deletes only a this file name, and a corresponding data block (file) only when the Inode node link number is reduced to 0 will be collected by the system.

Soft links (soft link): A is a soft link to B (both A and b are filenames), the Inode node number in A's directory entry is not the same as the Inode node number in directory item B, a and B point to two different inode, and then point to two different blocks of data. However, A's data block is only the path name of B (which can be used to find the directory entry for B). A and B are "master-slave" relationships, and if B is deleted, a still exists (because two are different files), but pointing to an invalid link.

2. Restrictions on use:

Hard links: A. You cannot create a hard link to a directory for several reasons, most importantly: The file system cannot have a link ring (".." When the directory was created). Except, this system can be identified), the consequences of the existence of the ring will cause such as file traversal and other operations such as chaos (du,pwd, such as the operation of the principle is based on file hard link, by the way, the second column of the ls-l result is also the number of hard links of the file, that is, the Inode node number of links)

B: You cannot create a hard link to a different file system, that is, two file names are under the same file system.

C: You cannot create a hard link to a nonexistent file, and the reason can be understood by the principle.

Soft Links: A. You can create a soft link to a directory, and the traversal operation ignores the soft link of the directory.

B: can cross file system

C: You can create a soft link to a nonexistent file, because it's just a string, and the string is a different thing for a real file.

3. Command

Hard: ln source file Link Name

Soft: ln-s source File Link name

The difference between hard links and soft links in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.