Hard link and soft link)

Source: Internet
Author: User
Tags inode usage
Preface

There are two types of link files under Linux. One is a file similar to the shortcut function of windows, which allows you to quickly link to the target file (or directory). This is a soft link; another method is to generate a new file name through inode links of the file system, instead of generating a new file! This is called hard link ). These two things are totally different! Now let's talk about it separately.

Hard Link (entity link, hard link or actual link)

In the previous section, we know several important pieces of information, including:

Each file occupies one inode, and the content of the file is directed by the inode record;
To read the file, you must point to the correct inode number through the file name recorded in the directory to read the file.
That is to say, the file name is only related to the directory, but the file content is related to inode. Can there be multiple file names corresponding to the same inode number? Yes! That is the origin of hard link. To put it simply, hard link only adds a link between a file name and an inode number in a directory.

For example, assume that my system has a/root/crontab object link of/etc/crontab, that is, these two files are linked to the same inode, naturally, all the information related to these two file names will be identical (except for the file name ). The actual situation can be as follows:

[Root @ WWW ~]#Ln/etc/crontab. <= create object link command[Root @ WWW ~]#Ll-I/etc/crontab/root/crontab1912701-RW-r -- 2 root Root 255 Jan 6 2007/etc/Crontab1912701-RW-r -- 2 root Root 255 Jan 6 2007/root/crontab

You can find that the two file names are linked to the inode number 1912701. So, are you sure the file permissions/attributes are identical? Because these two "file names" are actually the same "file! And you will also find that the second field is changed from the original 1 to 2! The field is called "Link". The meaning of this field is: "How many file names are linked to this inode number. If the correct data is read, the image is similar to the following:

Object link reading
You can find two different file names through the block specified by inode in the 1 or 2 directory, no matter which file name you use, you can refer to the real inode to read the final data! So what are the benefits? The biggest benefit is "security 』! If you delete any file name, inode and block still exist! At this time, you can read the correct file data through another "file name! In addition, no matter which file name you use for editing, the final result will be written to the same inode and block, so data can be modified!

In general, when you use hard link to configure the link file, the disk space and inode count will not change! We can see from the figure that hard link only writes one more connected data to the block in a directory, neither adding inode nor consuming the number of blocks!

Note:
In the creation of hard link, it is still possible to change the block of the system, that is, when you add this data but just fill up the block of the Directory, A new block may be added to record the file name connection, resulting in disk space changes! However, hard link usually uses a small amount of data, so it usually does not change the size of inode and disk space!

In fact, we can also know that hard link should be only available in a single file system, and should not be available across file systems! Because it is on the same filesystem! Therefore, there are limits on hard link:

Cannot span filesystem;
Cannot link the directory.

It cannot be understood across filesystems, so what is the problem of hard link to directories? This is because when hard link is used to link to a directory, the linked data needs to be linked together with all the data under the directory to be linked. For example, if you want to use the/etc object link to create a/etc_hd directory, in this case, all file names under/etc_hd need to create a hard link with the file names under/etc, instead of connecting to/etc_hd and/etc. In addition, if you need to create a new file under/etc_hd in the future, the data under/etc will have to create a hard link again, which will cause great environmental complexity. So, hard link currently does not support directories!

Symbolic Link (symbolic link, or shortcut)

Compared with hard link, symbolic link is much easier to understand. Basically, symbolic link is used to create an independent file, this file will let the data read the file name pointing to the file linked to it! Because the source file is only used as a pointing action, after the source file is deleted, the symbolic link file will "cannot open" and will always say "cannot open a file !』. The original file name cannot be found!

For example, create a symbolic link file to link to/etc/crontab:

 
[Root @ WWW ~]#Ln-S/etc/crontab crontab2[Root @ WWW ~]#Ll-I/etc/crontab/root/crontab21912701-RW-r -- 2 root Root 255 Jan 6 2007/etc/Crontab654687 lrwxrwxrwx 1 Root 12 Oct 22/root/crontab2->/etc/crontab

From the results in the above table, we can know that two files point to different inode numbers. Of course, two independent files exist! The important content of the link file is that it will write the "file name" of the target file. You can find out why the size of the link file in the above table is 12 bytes? The file name "/etc/crontab" on the right of the arrow (-->) has 12 English letters in total. Each English occupies 1 byes, so the file size is 12 bytes!

We will explain the preceding descriptions as follows:

Graph Symbol link file reading
Inode 1 reads only the file name to the link file. inode of the target file is obtained based on the Link name to the correct directory, and the correct data can be read. You can find that if the target file (/etc/crontab) is deleted, the entire process will fail, therefore, the link file cannot be read!

Note that the symbolic link and windows shortcut can be assigned an equal sign, and the files created by symbolic link are independent new files, so it will take up inode and block!

From the above description, it seems that hard link is safe, because even if the connected data under a directory is killed, it does not matter, as long as there is a connected data under any directory, the file will not be seen! For example, my/etc/crontab and/root/crontab point to the same file. If I delete the/etc/crontab file, the delete operation only removes the related data about crontab under the/etc directory. The inode and block of crontab are not changed!

However, since there are too many restrictions on hard link, including links that cannot be used as "directories", the usage is limited! But symbolic link is widely used! All right, it seems like you are almost dizzy! It doesn't matter. You will know what's going on after you implement it! To create a link file, you must use the ln command!

 

Ln
[Root @ WWW ~] #  Ln [-SF] Options and parameters: -S: If no parameter is added, the link is hard link- S is the symbolic link.  -F  : If the target file exists, remove it and create it again! Example 1: Set /Etc/passwd copy/ And observe inode and block [root @ WWW ~] #  CD/tmp [Root @ www tmp] #  CP-A/etc/passwd. [Root @ www tmp] #  Du-Sb; df-I. 18340. <= Pay attention to the capacity here! Filesystem inodes iused ifree iuse % Mounted on /Dev/hdc2 2560864 149738 2411126 6%/ #  Use du and DF to check the current parameter ~ The Du-sb  #  Is to calculate the total size of bytes under/tmp! 

 

Example 2: Create a hard link with/tmp/passwd to become a passwd-HD file, and observe the file and capacity.

[Root @ www tmp]#Ln passwd-HD[Root @ www tmp]#Du-Sb; df-I.18340. Filesystem inodes iused ifree iuse%Mounted on/Dev/hdc2 2560864 149738 2411126 6%/#After careful consideration, even if an additional file is under/tmp, the size of the entire inode and block has not changed![Root@ Www tmp]#Ls-il passwd *586361-RW-r -- 2 root Root 1945 Sep 29 02-21Passwd586361-RW-r -- 2 root Root 1945 Sep 29 passwd-HD#It turns out to point to the same inode! This is a key point! In addition, the number of links in the second column will also be added!

 

Example 3: Create a symbolic link for/tmp/passwd

[Root @ www tmp] #  Ln-s passwd-so [Root @ www tmp] #  Ls-Li passwd * 586361-RW-r -- 2 root Root 1945 Sep 29 02-21 Passwd 586361-RW-r -- 2 root Root 1945 Sep 29 passwd- HD 586401 lrwxrwxrwx 1 Root 6 Oct 22 passwd-so-> Passwd  #  Passwd-So points to different inode numbers! This is a new file ~ The content of this file is pointing  # Passwd. The size of passwd-so is 6 bytes, because passwd contains six characters.  [Root @ Www tmp] #  Du-Sb; df-I. 18346 . Filesystem inodes iused ifree iuse % Mounted on /Dev/hdc2 2560864 149739 2411125 6%/ #  Call! The total capacity and inode usage change ~ Indeed! 

 

Example 4: Delete the source file passwd. Can the other two files be enabled?

 [root @ www tmp] #  RM passwd  [root @ www tmp] #  CAT passwd-HD   ...... displayed normally! [Root  @ www tmp] #  CAT passwd-So  CAT: passwd- so: no such file or directory [root  @ www tmp] #  ll passwd * -RW-r -- 1 Root 1945 Sep 29 passwd- hdlrwxrwxrwx  1 Root 6 o CT 22 passwd-so->  passwd  #  sorry! The symbolic link cannot be enabled! If the target file of the symbolic link does not exist,  #  In fact, some of the file names are displayed in special colors!  

Please note! If ln is used without any parameters, it is hard link! As in example 2, after hard link is added, you can find that when LS-L is used, the attribute of the link column displayed is added! What will happen if passwd is cut down at this time? The content of passwd-HD will be the same as that of passwd, but passwd-So will not find this file!

However, if ln uses the-S parameter, it means "shortcut" under windows. When you modify the symbolic link file in Linux, it is actually the "original file", so no matter where your original file is linked, as long as you modify the link file, the original file is changed! For example, if you use the-S parameter to create a file named passwd-so, when you modify passwd-so, the content of the file is exactly the same as that of passwd, after you press save, the file passwd will be changed!

In addition, if you do the following link:

Ln-S/bin/root/bin
If you enter the/root/bin directory! This directory is actually the/bin directory, because you have made a link file !』 So if you go to the/root/bin directory of the link you just created and kill the data in it, um! The data in/bin is lost! Please pay attention to this! So use "RM/root/bin" to delete this link file!

Basically, symbolic link is widely used, so pay special attention to the usage of symbolic link! It will be often used in the future!

Link quantity of directories:
You may have discovered that, when we use hard link for "File Link", we can find that the second field displayed in LS-l will be added to the pair, what is the default number of links when a directory is created? Let's think about what at least exists in an "empty directory? Haha! There are two directories, "." and! When we create a new directory named/tmp/testing, there are basically three things:

/Tmp/Testing
/Tmp/testing /.
/Tmp/testing /..
Among them,/tmp/testing and/tmp/testing/. are actually the same! All represent this directory ~ And/tmp/testing /.. it indicates the directory/tmp. Therefore, when we create a new directory, the number of links in the new directory is 2, the number of links in the directory at the upper level will be added to 1 ". If you do not believe this, let's test it:

[Root @ WWW ~]#Ls-LD/tmpDrwxrwxrwt 5 root Root 4096 Oct 22 14: 22/TMP [root@ WWW ~]#Mkdir/tmp/testing1[Root @ WWW ~]#Ls-LD/tmpDrwxrwxrwt 6 Root 4096 Oct 22 14: 37/TMP [root@ WWW ~]#Ls-LD/tmp/testing1Drwxr-XR-x 2 root Root 4096 Oct 22/tmp/testing1

Look! The number of links in the above-level directory/tmp is changed from 5 to 6, and the number of links in the new directory/tmp/testing is 2. Can you understand the meaning of the number of links in the directory? Pai_^

From http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem_2.php

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.