Linux system hard links and soft links

Source: Internet
Author: User

background:

When several users are working in a project. You often need to share files.

Suppose a shared file is present in a different folder that belongs to a different user at the same time. It is very convenient to work.

For example, the B and C folders have a file D is a shared file that both can access and change, this is very convenient, but there are some problems, assuming that the folder includes the disk address, when the file is connected. The disk address in the C folder must be copied to the B folder, and if B or C is subsequently added to the file, the new data block will only be included in the folder of the user who is working on the join.

Other users are unaware of this change. Violates the purpose of sharing.

Two ways to solve this problem.

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

Generate a new file name through the Inode link of the filesystem. Instead of generating new files! This is also known as the entity link (hardlink).

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

· Want to read the file.    The required entry is to go through the file name of the folder record to point to the correct inode number for the ability to read. In other words, the file name is only related to the folder, but the archive 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 is simply a new file name linked to an inode number in a folder login.
[

Give me a sample, for example. If my system has a/root/crontab he is a/etc/crontab entity link, which means that the two files are linked to the same inode, naturally all the relevant information for the two file names will be identical (except for the file name). The actual situation can be seen as follows:


You can find two file names linked to 1912701 of this inode number. So you see. is the permissions/attributes of the file all the same? Because these two "file names" are in fact identical "archives"! And you will find that the second field is 2 from the original 1. That field is called a "link." The meaning of this field is: "How many file names are linked to this inode number".

Assume that you will read the correct data in a way that is drawn. Just like the following screen:


You can find two different file names through the inode specified in the 1 or 2 folder, regardless of which file name you use can refer to real that inode to read the data finally! So what are the advantages? The biggest advantage is "safety"!

As in, let's say you delete whatever "file name" you have, in fact the inode and the block are still there!

At this point you can read the correct profile data through a "file name"! In addition, regardless of which "file name" you use to edit, the result will be written into the same inode and block. Therefore, the data can be changed. Overcome the above problems, 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 look at Figure 2.2.1, as we can tell by the figure. Hard link simply writes a more connected data to a block in a folder. Neither the Inode nor the block count will be added.

Tips:

To put it simply: A hard link is the same file with multiple aliases (with a common inode), and different files point to the same inode. To achieve the purpose of file sharing;

Strengths: Security, change synchronization. Deleting a hard-link file does not affect other files that have the same inode number;

Cons: Cannot cross Filesystem, cannot link folder.

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. It should not be cross-file system!

Because the figure 2.2.1 is on the same filesystem! So there is a limit to hard link.

Cannot link folder: Because the linked data needs to be linked to all the data under the link login, for example, if you are using hard link to link to login, suppose you want to use/etc to create a/ETC_HD login with entity links. All the/ETC_HD under the same time with/etc under the file name to establish hard link. Instead of just linking to/ETC_HD and/etc.

Moreover, the future hypothesis needs to be under the/ETC_HD resumes the new file, the joint/etc and so on the data must establish once hard link, therefore causes the environment to be quite big complexity. So you cannot link the folder.

Hard Link In the production, in fact, it is possible to change the system block. That's when you add this number
It just happens to fill the block of the folder. It is possible to add a new block to record the file name connectivity.
And the change of disk space is found. It's just that. The amount of connected data used by hard link is very small. So usually
Does not change the size of the inode and disk space!

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

As opposed to hard link, symbolic link is much better understood, 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 the act of pointing with the mere use of a file, 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:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

We can know that two files point 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. As the arrow (--) to the right, the filename "/etc/crontab" always has 12 English. Each English occupies 1 byes, so the file size is 12bytes. For the above explanations, we will explain them for example:


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 finally be able to read the correct data. What you can find is that. Assuming that 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! In particular, this symbolic link with Windows can be equated with the file created by symbolic link as a separate new file. So it takes up the inode and block! See above for instructions. It seems that hard link is safer. Because even if the login under a certain eye is killed, there is no relationship, just need to have any login under the data, the file will not disappear! To take the example above, my/etc/crontab and/root/crontab point to the same file, assuming I deleted/etc/crontab this file, the deletion of the action is in fact only to have/etc login about the crontab of the connected data removed, The inode and block where the crontab is located are virtually unchanged.

Soft Link: The valid content in the file user data is a pointer to the path name of the file. is an ordinary file, the data block is a bit special. Deleting a soft link does not affect the source file.

Advantages: Convenient, able to use the folder.

Simply provide the network address of a machine and the path where the file resides on that machine. To connect the files on any machine anywhere in the world.

Cons: Additional overhead is required. You must read the file that includes the path, and then the scan path of 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.

Because the hard-link app is restricted. Soft link application is more extensive.



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.