Hard links and soft connections [turn from Vbird]

Source: Internet
Author: User

Preface

There are two kinds of links under Linux, one is similar to the Windows shortcut function file, you can quickly link to the target file (or directory), this is a soft link, the other is through the file system Inode link to generate new file name, not to produce new files! This is called hard link. These two kinds of things are totally different! Now let's talk about it separately.


Hard Link (entity links, rigid links or actual links)

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

Each file occupies an inode, and the contents of the file are pointed to by the inode record;
To read the file, you must go through the directory record's file name to point to the correct inode number to read.
In other words, the file name is only related to the directory, but the contents of it are related to the inode. So 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 simply adds a file name to a connected record in a directory linked to an inode number.

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:

[Email protected] ~]# Ln/etc/crontab. <== command to create entity links [[email protected] ~]# ll-i/etc/crontab/root/crontab1912701-rw-r--r--2 root root 255 Jan 6 2007/etc/c rontab1912701-rw-r--r--2 root root 255 Jan 6 2007/root/crontab

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 name" is actually identical "document"! 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:


File Reads for entity links
means that you can find two different file names from the block specified in the inode of 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 file 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!

In general, when you use hard link to configure a linked file, the disk space and the number of inode will not change! We still see from the figure, as can be seen from the figure, hard link is only in a directory of the block more than write a connected data, neither add inode or block number of miles!

Note:
Hard link in the production, in fact, it is possible to change the system block, that is, when you add this data but just to fill the directory block, it is possible to add a block to record the file name of the connection, resulting in disk space changes! However, the amount of connected data used by hard link is very small, so it usually doesn't change the size of the inode and disk space.


In fact, 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 it's on the same filesystem! So hard link is limited:

Cannot cross Filesystem;
Cannot link directory.

Can not cross the Filesystem good understanding, that can not hard link to the directory is how to go? This is because if you link to a directory using hard link, the linked data needs to be linked together with all the data under the linked directory, for example, if you want to create a/ETC_HD directory with/etc using entity links, then all the files under/ETC_HD are associated with/ET The files under C must be created hard link instead of just linking to/ETC_HD and/etc. And, in the future if you need to create a new file under the/ETC_HD, and so on, and so on, etc, the data will have to create a hard link, resulting in a considerable degree of complexity of the environment. So, at present, hard link for the directory is not supported for the moment Ah!


Symbolic Link (symbolic link, i.e. shortcut)

Compared to hard link, symbolic link can be a good understanding, basically, symbolic link is to create a separate file, and this file will let the data read to the file of his link's filename! Since the file is only used to point to the action, so when the source file is deleted, Symbolic Link'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 first create a symbolic link file to link to/etc/crontab to see:

[Email protected] ~]# ln-s/etc/crontab crontab2[[email protected] ~]# ll-i/etc/crontab/root/crontab21912701-rw-r--r --2 root root 255 Jan 6 2007/etc/crontab654687 lrwxrwxrwx 1 root root Oct 13:58/root/crontab2-/etc/crontab

As a result of the above table we can know that two files point to different inode numbers, of course, two separate files exist! And the important part of the link is that he will write the "filename" 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:


The file read of the graph symbol link
From the 1th inode, the contents of the link file are only file name, link to the correct directory according to the file name to obtain the inode of the target file, and finally be able to read the correct data. What you can find is that if the target file (/etc/crontab) is deleted, then the whole link will not go on, so there will be 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 is a separate new file, so will take up the inode and block Oh!


From the above instructions, it seems that hard link is more secure, because even if the connected data in a directory is killed, there is no relationship, as long as there is any directory of connected data, then the file will not disappear! In 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 directory about the crontab of the connected data removed, Cront The inode and block where AB is located are not actually changed!

However, because there are too many restrictions on hard Link, including the inability to do the "directory" link, so the use is more limited! On the contrary, symbolic Link is more widely used. Well, say the hype, see you are almost faint! Don't worry, just do it and know what's going on! To make a link file, you have to use ln as a command!

LN
[[Email protected] ~]# LN [-SF] Source file destination file options and Parameters:-S: If you do not add any parameters to the link, it is hard link, as for-S is symbolic link-f: If the target file exists, the target will be active Files are removed and then created! Example one: Copy the/etc/passwd to/TMP and observe the inode and Block[[email protected] ~]# cd/tmp[[email protected] tmp]# cp-a/etc/passwd. [Email protected] tmp]# DU-SB; Df-i. 18340. <== first of all, pay attention to how much capacity here! Filesystem inodes iused IFree iuse% mounted on/dev/hdc2 2560864 149738 2411126 6%/# use Du and DF to check the current parameters ~ that DU-SB # is calculated How many bytes capacity is there in the whole/tmp?

Example two: Making/tmp/passwd hard link into a PASSWD-HD file and observing the file and capacity

[Email protected] tmp]# ln passwd passwd-hd[[email protected] tmp]# DU-SB; Df-i. 18340. Filesystem inodes iused IFree iuse% mounted on/dev/hdc2 2560864 149738 2411126 6%/# Look closely, even if there is a file in/tmp, the entire inode and bloc K's capacity has not changed!  [Email protected] tmp]# Ls-il passwd*586361-rw-r--r--2 root root 1945 Sep 02:21 passwd586361-rw-r--r--2 root root 1945 Sep 02:21 passwd-hd# originally point to the same inode Ah! That's a point! In addition, the number of links in the second column will also be added!

Example three: Create a symbolic link to/tmp/passwd

[[email protected] tmp]# ln-s passwd passwd-so[[email protected] tmp]# Ls-li passwd*586361-rw-r--r--2 root root 1945 s EP 02:21 passwd586361-rw-r--r--2 root root 1945 Sep 02:21 passwd-hd586401 lrwxrwxrwx 1 root root 6 Oct 14:18 pa Sswd-so---passwd# passwd-so points to the inode number is different! This is a new file ~ The content of this file is pointing to # passwd. Passwd-so size is 6bytes, because passwd a total of six characters [[email protected] tmp]# DU-SB; Df-i. 18346. Filesystem inodes iused IFree iuse% mounted on/dev/hdc2 2560864 149739 2411125 6%/# hoo! The entire capacity and the number of inode uses have changed ~ Yes!

Example four: Delete source file passwd, can the other two files be opened?

[Email protected] tmp]# RM passwd[[email protected] tmp]# cat Passwd-hd ... Normal display finished! [[email protected] tmp]# cat passwd-socat:passwd-so:no such file or Directory[[email protected] tmp]# ll Passwd*-rw-r--r --1 root root 1945 Sep 02:21 passwd-hdlrwxrwxrwx 1 root root 6 Oct 14:18 passwd-so-passwd# afraid of it! The symbolic link does not open! In addition, if the target file of the symbolic link does not exist, the part of the file name will have a special color display!


Be careful! Use LN If you don't add any parameters, then it's hard Link! As in case two, after adding hard link, you can find that the link property that is displayed when using Ls-l is added! And if this time cut off passwd what will happen? PASSWD-HD content will be the same as the original passwd, but Passwd-so will not find the file!

If LN uses the-s argument, it is almost the "shortcut" under Windows. When you modify the symbolic link file under Linux, the change is actually "original file", so no matter where your original file is linked to, as long as you modify the link file, the original file will be changed! For example, because you use the-s parameter to create a file named Passwd-so, then when you modify PASSWD-SO, its content is exactly the same as passwd, and when you press save, the change will be passwd this file!

Also, if you do a link like this below:

Ln-s/bin/root/bin
So if you enter/root/bin this directory, "please note yo!" This directory is actually/bin this directory, because you have made the link file! "So, if you go into/root/bin this newly created link directory and kill the data in it, eh!" The data inside the/bin is gone! Please pay attention to this point! So hurry up and use "rm/root/bin" to remove this link file!

Basically, the use of symbolic link is relatively wide, so you should pay special attention to the use of symbolic link! The future will always be used again!


Link number on the catalog:
Perhaps you have found that, when we make "file link" in hard link, we can find that the second field shown in Ls-l will add a pair, then ask, if you create a directory, his default link number will be what? Let's think about the existence of at least one "empty directory". Oh! Is there. And.. These two directories Ah! Well, when we create a new directory named/tmp/testing, there are basically three things:

/tmp/testing
/tmp/testing/.
/tmp/testing/.
And among them/tmp/testing and/tmp/testing/. In fact, the same! All represent the directory Ah ~ and/tmp/testing/. Represents the/tmp directory, so that when we create a new directory, "The new directory link number is 2, and the link number of the previous level will be added 1" If not, let's take a test to see:

[Email protected] ~]# LS-LD/TMPDRWXRWXRWT 5 root root 4096 Oct (14:22/tmp[[email protected] ~]# mkdir/tmp/testing1[ [Email protected] ~]# LS-LD/TMPDRWXRWXRWT 6 root root 4096 Oct (14:37/tmp[[email protected] ~]# ls-ld/tmp/testing1d Rwxr-xr-x 2 root root 4096 Oct 14:37/tmp/testing1

Look! The original so-called top-level directory/TMP link number is added from 5 to 6, as for the new directory/tmp/testing is 2, so you can understand the meaning of the number of link in the directory? ^_^

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

Hard links and soft connections [turn from Vbird]

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.