There are two special "files" in the Linux system, soft links and hard links.
Soft links can be thought of as shortcuts in Windows that allow you to quickly link to a target file or directory.
A hard link generates a new filename through the inode of the file system instead of generating a new file.
The creation method is very simple:
Soft link (symbolic link) ln -s source target
Hard link (physical link) ln source target
InodeTo explain the difference and connection between the two needs to first explain the inode in the Linux file system. When partitioning a disk partition and formatting it, the entire partition is divided into two parts, the inode area and the data block (the actual data is placed in the data area). The inode is the (directory, archive) file in a file system. The unique identifier of the file must be found and read before the file needs to be accessed. Inode stores many important parameters of the file, the unique identifier is called Inumber, other information also has creation time (ctime), modification time (mtime), file size, owner, belonging user group, read and write permissions, data. Block number and other information.
Usually the number of inodes is arranged according to the purpose of the partition (this is another topic). For example, if the number of files is large and the files are very small, you need to increase the inode size so that all files can be indexed. Otherwise it will happen that this partition is not full and cannot be written to any file.
Directory file and archive file
Directory file: record the file name in this directory
Archive file: record actual file data
The inode itself does not record the file name. The file name is recorded in the block of the directory file, so adding, deleting, and changing the file name are related to the W permission of the directory. Therefore, when we want to read a file, we must go through the inode and block of its directory, and then we can find the inode number of the file to be read, and finally read the data in the correct file block. The system locates each file through an inode (rather than a file name).
Directory inode (meeting permissions?) => directory block => file inode (meeting permissions?) => file block
Hard link
Multiple file names correspond to the same inode. A hard link simply adds a file name to a related record of an inode number in a directory. If any of the file names in the above figure are deleted, the inode and block of the file still exist, and the correct file data can still be read by another file name. In addition, no matter which file name is used for editing, the final result will be written to the same inode and block, so the data can be modified.
Soft connection
A soft connection is to create a separate file, and this file will make the data read to the file name of the link of its link. Because it is only a pointing action, the soft connection file cannot be opened after the source file is deleted. Because the original filename was not found. The content of the link file is only the file name. According to the file name, the link to the correct directory to further obtain the inode of the target file, and finally the correct data can be read. If the original file name of the target file is deleted, the entire link will not proceed.
The following uses an example to illustrate hard links and soft links.
There are now two files in the directory, one named AAA and one named BBB. Quote $ ls -il963922 -rw-r--r-- 1 ocean ocean 92 2007-05-18 15:46 AAA963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB
First make a hard link to AAA. Quote $ ln AAA AAAhard$ls -il963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAA963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15 :46 AAAhard963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB
Here we note that before creating the link, the number of links displayed by AAA is 1. After the link is created, the number of links between 1.AAA and AAAhard becomes 2. 2. The inode numbers of AAA and AAAhard are the same, both are 963922. The file sizes displayed by AAA and AAAhard are the same, both are 92B.
It can be seen that the operation of the ln command is performed: AAA and AAAhard are two names of the same file, they have the same index node number and file attributes, and a hard link for establishing the file AAA is the file index node of the AAA in the current directory. Create a new pointer. You can delete any one of them, such as rm AAA, only one pointer will be deleted at a time, and the number of links will be decremented by one at the same time. Only when all the pointers to the contents of the file, that is, the number of links is reduced to 0, the kernel will take the contents of the file from Removed on disk.
Although hard links save space, it is also the traditional way for Linux systems to integrate file systems, but there are some shortcomings: 1. It is not allowed to create hard links to directories. 2. It is not possible to establish links between files of different file systems. Because inode is the index value of this file in the current partition, relative to this partition, of course, can not cross the file system.
Then we make a soft link to the BBB, the soft link overcomes the lack of hard links, without any file system restrictions, any user can create a symbolic link to the directory. It is now more widely used, it has more flexibility, and can even link files across different machines and different networks. Quote $ ln -s BBB BBBsoft$ ls -il total usage 0963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAA963922 -rw-r--r-- 2 ocean ocean 92 2007 -05-18 15:46 AAAhard963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB963924 lrwxrwxrwx 1 ocean ocean 3 2007-05-18 15:47 BBBsoft -> BBB
The results from the above link can be seen as soft links and hard links. The difference is not only conceptually, but also completely different in implementation.
The difference: 1. Hard link original file / link file common an inode number, indicating that they are the same file, and the soft link original file / link file has different inode numbers, indicating that they are two different files; 2. in the file The soft link on the attribute clearly writes the link file, and the hard link is not written, because in essence the hard link file and the original file are completely equal; 3. The number of links is different, the number of links of the soft link will not be Increase; 4. The file size is different, the size of the hard link file is the same as the original file. The size of the soft link display here is different from the original file. The BBB size is 95B, and BBBsoft is 3B. Because BBB has 3 characters
5. Soft links do not have any file system restrictions, any user can create a symbolic link to the directory
In short, the establishment of a soft link is to create a new file. When accessing a linked file, the system will find that it is a linked file, which reads the linked file to find the file that is actually being accessed.
Of course, soft links also have the disadvantage of hard links: because the link file contains the path information of the original file, when the original file is moved from one directory to another, and then the link file is accessed, the system can't find it, and the hard link is Without this flaw, how do you want to move it; and it has to allocate extra space for the system to create new index nodes and save the path to the original file.
Medical Equipment Battery,Cr123 Batteries,Lithium Battery 123,Cr2 Lithium Battery
Jiangmen Hongli Energy Co.ltd , https://www.honglienergy.com