Linux system hard links and soft links

Source: Internet
Author: User

background:

When several users work in a project, they often need to share files. If a shared file appears at the same time in different directories belonging to different users, it is convenient to work. For example, the B and C directories have a file d is a shared file that both can access and modify, this is very convenient, but there are some problems, if the directory contains a disk address, when the connection file, the C directory must be copied to the disk address in the B directory, if B or C then add content to the file, The new data block will only be included in the directory of the user who is adding work. Other users are unaware of this change and are violating the purpose of sharing. Two ways to solve this problem.

method One: Hard links (entity connection or actual connection)

Generate new file names through the inode link of the filesystem, rather than generating new files! This is also known as entity linking (Hardlink).

· Each file occupies an inode, and the contents of the file are pointed to by the Inode Records;

·    If you want to read the file, the required entry must go through the file name of the directory record to point to the correct inode number to read. In other words, the file name is only related to the directory, but the file content is related to the inode. Think about it, is it possible to have multiple file names corresponding to the same inode number? Some! That's the origin of hard link. So simply put: Hard link just adds a file name in a directory linked to an inode number login.
[

For example, suppose I have a system with a/root/crontab. He is a/etc/crontab entity link, which means that the two files are linked to the same inode, and naturally all relevant information for the two filenames will be identical (except for the file name). The actual situation can be as follows:


You can see that the two file names are linked to the 1912701 inode number, so you see if the permissions/properties of the file are exactly the same? Because these two "file names" are actually identical "archives"! And you will find that the second field is 2 from the original 1! The field is called a "link" and the meaning of this field is "How many file names are linked to this inode number". If you're drawing the correct data, you'll see a picture like this:


You can find two different file names through the inode specified in the 1 or 2 directory, regardless of which file name you use can refer to real that inode to read the final data! So what good is that? The biggest benefit is "safety"! As in, if you delete any of the "file name", in fact, the inode and block are still there! You can now read the correct profile data through another "file name"! In addition, regardless of which "file name" you use to edit, the final result will be written to the same inode and block, so the data can be modified! Solve the above problem, in general, when using hard link to set the link file, the disk space and the number of inode will not be changed! We still see from figure 2.2.1, as can be seen from the figure, hard link is only in a directory of the block more write a connected data, do not increase the inode will not consume block number.

Tips:

Simply put: A hard link is the same file that uses multiple aliases (with a common inode), which is different files pointing to the same inode. To achieve the purpose of file sharing;

Advantages: Security, modify synchronization, delete a hard link file does not affect other files that have the same inode number;

disadvantage: Cannot cross Filesystem, cannot link directory.

Can not cross filesystem: by the figure 2.2.1 Actually we can also know, in fact, hard link should be only in a single file system, should not be able to cross the file system! Because the figure 2.2.1 is on the same filesystem! So hard link is limited;

Cannot link directory: Because if you use hard link to link to the login, the linked data needs to be linked with all the data under the linked login, for example, if you want to use/etc to establish a/ETC_HD login with entity links, then/ETC_HD All of the below files are also created hard link with the files under/etc, instead of just linking to/ETC_HD and/etc. And, in the future if you need to resume a new file under/ETC_HD, the data under the joint/etc will have to create a hard link, resulting in a considerable degree of complexity in the environment. Therefore, the link directory cannot be.

Hard Link In the production, in fact, it is possible to change the system block, that is, when you add this number
It is possible to add a block to record the file name connection when the block of the directory is filled.
And find the changes in disk space! However, the amount of connected data used by hard link is very small, so it is usually
Does not change the size of the inode and disk space!

method Two: Soft link (symbolic connection is a shortcut)

Compared to hard link, symbolic link can be a good understanding, basically, symbolic link is in the establishment of a separate file, and this file will let the data read to the file of his link that file name! By using the file as a point of action, so, when the source file is deleted, Symboliclink's file will "not open", will always say "unable to open a file!" 』。 In fact, the original "file name" is not found! For example, let's set up a symbolic link file to link to/etc/crontab to see:


We can know two files pointing to different iode numbers, of course, two separate files exist! And the important part of the link is that he will write the "file name" of the target file, and you can find out why the size of the link file in the table is bytes. Because the arrow (--) to the right of the file name "/etc/crontab" total of 12 English, each English occupies 1 byes, so the file size is 12bytes! For the above explanations, we explain them as follows:


From the 1th inode read to the contents of the link file only file name, according to the file name link to the correct login to obtain the target file Inode, and eventually be able to read the correct data. What you can find is that if the target file (/etc/crontab) is deleted, the whole link will not go on, so there is a problem that cannot be read through the link file! Here still have to pay special attention, this symbolic link and Windows shortcut can be equal to him, the file created by symbolic link as a separate new file, so will occupy the inode and block Oh! From the above instructions, it seems that hard link is more secure, because even if a login under the data is killed, there is no relationship, as long as there is any one login under the connected data, then the file will not disappear! To cite the above example, my/etc/crontab and/root/crontab point to the same file, if I delete the/etc/crontab this file, the deletion of the action is actually just the/etc login about the crontab of the connected data removed it, The inode and block where the crontab is located are in fact unchanged.

Soft Link: File user data Valid content is the path name of another file point, is a normal file, data block a bit special. Deleting a soft link does not affect the source file.

Advantages: Convenient, can be used in the directory. Simply providing the network address of a machine and the path where the file resides on the machine can be connected to the files on the machine anywhere in the world.

Cons: Additional overhead is required. You must read the file that contains the path, and then take a scan path for a portion of a part until the Inode node is found and additional disk access is required. Delete source files, link files to dead files.

Soft links are widely used because hard-link applications are limited.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux system hard links and soft links

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.