A summary of hard links and soft links for files and directories in Linux

Source: Internet
Author: User
Tags hash parent directory file permissions

Hard Links
The new file is an alias for a file that already exists, and the new file is still available when the original file is deleted.
Hard Link Features:
1, with the same I node and the same storage block fast, can be seen as the same file
2, can be identified through the I node, I node number is the same
3, not across partitions
4, not for directory use
5, delete files, the normal use of hard links

Soft links
Another connection, called a symbolic connection (symbolic link), is also called a soft connection. Soft link files have shortcuts similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains the location information for another file.
Soft Link Features:
1, similar to Windows shortcuts
2, the soft link has its own I node and block fast, but the data blocks to save only the original file name and I node number, and no actual file data
3, lrwxrwxrwx l soft link, soft link file permissions are rwxrwxrwx, but the specific view of the original file permissions
4, modify any file, the other is changed
5, delete the original file, soft link can not be used
6, create soft link must write absolute path
Note the soft link's file permissions are 777 but the final permission has the source file decision L represents the soft link file.
Once the source file is deleted, the soft link will not be available. (This is the main difference from hard links)
Note: When you create a soft link, the source file always uses an absolute path, otherwise the source file cannot be found if the soft link is created and the source file is not in the same directory. (Hard links do not have this situation)
Less restrictive, more flexible, recommended for use.

Add:
Hard connections in Linux systems have two limitations: they cannot cross file systems and do not allow normal users to make hard connections to the directory. As for the first restriction, it is well understood, and the second one is not so well understood. Using the ls-l command for any directory can see that the number of connections is at least 2, which means that there is a hard connection in the system, and that the command ln-d can also have the superuser make a hard connection to the directory, which means that the system limits the hard connection to the directory is only a hard rule, is not logically impermissible or technically not possible. So why does the operating system have to be limited? There may be two answers.

First of all, if you introduce a hard connection to a directory, it is possible to introduce a loop into the directory, and the system will fall into an infinite loop as the directory traverses. Perhaps you would say that symbolic connections can not also introduce loops, so why not restrict the symbolic connection of the directory? The reason is that in the Linux system, each file (directory is also a file) corresponds to an inode structure, where the Inode data structure contains the file type (directory, common files, symbolic connection files, etc.) information, that is, the operating system in the traversal directory can be judged by the symbolic connection, Since it is possible to determine the symbolic connection can certainly take some measures to prevent the entry into the excessive cycle, the system in a row after the 8 symbolic connection stop traversal, which is why the directory symbol connection will not enter the dead loop reason. However, for hard connection, due to the data structure and algorithm limitations adopted in the operating system, it is not possible to prevent this kind of dead loop at present.

Before explaining the second reason, let's look at what the dentry structure of the file looks like in the system space and how it is stored in the system space. The dentry structure includes the file name, the inode number of the file, pointers to the parent directory Dentry structure, and other pointers that are irrelevant to this discussion, where the key is the pointer to the parent directory; All dentry structures in the system are stored in hash tables by hash value, The hashing algorithm here is very important, which is to hash the hash value by taking the address of the file name and the parent directory of the dentry structure. Now suppose there are two directories/A and/b, where/b is the hard connection to/A that we have established through the ln-d command. This time in the kernel space there will be a/a dentry structure and A/b dentry structure, from the above knowledge, the/A and/b directory each of the files or directories have their respective dentry structure (because although the file name under the/a directory has not changed, However, because the dentry structure has a pointer to the parent directory Dentry and computes a hash value that takes into account the address of the parent directory Dentry structure, the dentry structure is at a loss at this time, and the inheritance also affects the files underneath all subdirectories, which wastes a lot of system space. This is especially true if there are a large number of files and subdirectories in a hard-connected directory. This may be the second reason.

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.