2692 views|0 replies

127

Posts

0

Resources
The OP
 

Hard links and soft links in Linux file system [Copy link]

There are two special "files" in Linux system: soft links and hard links. Soft links can be regarded as shortcuts in Windows, which allow you to quickly link to the target file or directory. Hard links generate new file names through the file system's inode instead of creating new files. The creation methods are very simple: 1. Soft link (symbolic link) ln -s source target 2. 3. Hard link (physical link) ln source target 4. inode To explain the difference and connection between the two, we need to first explain the inode in the linux file system. When the disk partition is divided and formatted, the entire partition will be divided into two parts, namely the inode area and the data block (the actual data is placed in the data area). The inode is the unique identifier of the (directory, archive) file in a file system. When you need to access this file, you must first find and read the inode of this file. Inode stores many important parameters of the file, among which the unique identifier is called Inumber, and other information includes creation time (ctime), modification time (mtime), file size, owner, user group, read/write permissions, block number where data is located, etc. Usually the number of inodes is arranged according to the purpose of the partition (this is another topic). For example, if there are many files and they are all small, the inode number needs to be increased to be larger so that all files can be indexed. Otherwise, the partition will not be full and no file can be written. Directory files and archive files Directory files: record the file names under the directory Archive files: 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. Therefore, 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 block of the file. The system locates each file through the index node (not the file name).
Directoryinode(Permissions satisfied?) => Directoryblock => Fileinode(Permissions satisfied?) => Fileblock
Hard link
file:///C:\Users\郭晓娟\AppData\Local\Temp\ksohtml\wpsBD0E.tmp.pngMultiplefile names correspond to the sameinode. Hard links are just association records of a file name linked to a certaininodenumber in a certain directory. If any of the file names in the above figure is deleted, the file'sinodeandblockwill still exist, and the correct file data can still be read through another file name. In addition, no matter which file name is used for editing, the final result will be written to the sameinodeandblock, so data can be modified.
soft link
file:///C:\Users\Guo Xiaojuan\AppData\Local\Temp\ksohtml\wpsBD1E.tmp.png
Soft link is to create an independent file, and this file will point the data reading to the file name of the file it links to. Since it only acts as a pointing action, when the source file is deleted, the soft link file cannot be opened because the original file name cannot be found. The content of the link file is only the file name. According to the file name, it links to the correct directory and further obtains 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, then the whole process cannot proceed.
The following example is used to illustrate hard links and soft links.
There are now two files in the directory, one named AAA and the other named BBB]. Quote $ ls -il 963922 -rw-r--r-- 1 ocean ocean 92 2007-05-18 15:46 AAA 963923 -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 -il 963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAA 963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAAhard 963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB Here we should note that before the link is created, the number of links displayed by AAA is 1. After the link is created, 1.The number of links of AAA and AAAhard are both changed to 2. 2.The inode numbers of AAA and AAAhard are the same, both are 963922. 3.The file sizes displayed by AAA and AAAhard are also the same, both are 92B. It can be seen that the result of the operation of the ln command is: AAA and AAAhard are two names of the same file, they have the same index node number and file attributes. To create a hard link for the file AAA is to create a new pointer for the file index node of AAA in the current directory. It is easy to learn embedded systems. You can delete any of them, such as rm AAA, each time only one pointer will be deleted, and the number of links will be reduced by one. Only when all pointers to the file content, that is, the number of links, is reduced to 0, the kernel will delete the file content from the disk.
Although hard links save space and are also the traditional way of integrating file systems in Linux systems, there are some shortcomings: 1. It is not allowed to create hard links for directories. 2.You cannot create links between files in different file systems. Because inode is the index value of the file in the current partition, which is relative to this partition and cannot cross file systems.
Then we make a soft link pointing toBBB. Soft links overcome the shortcomings of hard links and do not have any file system restrictions. Any user can create a symbolic link pointing to a directory. Therefore, it is now more widely used. It has greater flexibility and can even link files across different machines and networks. Quotes $ ln -s BBB BBBsoft $ ls -il Total usage 0 963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAA 963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAAhard 963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB 963924 lrwxrwxrwx 1 ocean ocean 3 2007-05-18 15:47 BBBsoft -> BBB
From the results of the above link, we can see thatthe difference between soft links and hard links is not only in concept, but also in implementation.
Difference: 1.The original file of the hard link/The linked file shares the sameinodenumber, indicating that they are the same file, while the original file of the soft link/The linked file has differentinodenumbers, indicating that they are two different files; 2.In the file attributes, the soft link clearly states that it is a linked file, while the hard link does not, because in essence the hard link file and the original file are completely equal; 3.The number of links is different, and the number of links of the soft link will not increase; 4. The file sizes are different. The size of the hard link file is the same as the original file. The size of the soft link is different from the original file. The size of BBB is 95B, while BBBsoft is 3B. Because BBB has 3 characters in total 5. Soft links do not have any file system restrictions. Any user can create a symbolic link pointing to a directory. In short, creating a soft link is to create a new file. When the link file is accessed, the system will find that it is a link file and read the link file to find the real file to be accessed.
Of course, soft links also have disadvantages that hard links do not have: Because the link file contains the path information of the original file, when the original file is moved from one directory to another, the system cannot find it when accessing the link file. Hard links do not have this defect and can be moved as you wish. In addition, it requires the system to allocate additional space to create a new index node and save the path of the original file.
The file sizes are different. The size of the hard link file is the same as the original file. The size of the soft link is different from the original file. The size of BBB is 95B, while BBBsoft is 3B. Because BBB has 3 characters in total 5. Soft links do not have any file system restrictions. Any user can create a symbolic link pointing to a directory. In short, creating a soft link is to create a new file. When the link file is accessed, the system will find that it is a link file and read the link file to find the real file to be accessed.
Of course, soft links also have disadvantages that hard links do not have: Because the link file contains the path information of the original file, when the original file is moved from one directory to another, the system cannot find it when accessing the link file. Hard links do not have this defect and can be moved as you wish. In addition, it requires the system to allocate additional space to create a new index node and save the path of the original file.
The file sizes are different. The size of the hard link file is the same as the original file. The size of the soft link is different from the original file. The size of BBB is 95B, while BBBsoft is 3B. Because BBB has 3 characters in total 5. Soft links do not have any file system restrictions. Any user can create a symbolic link pointing to a directory. In short, creating a soft link is to create a new file. When the link file is accessed, the system will find that it is a link file and read the link file to find the real file to be accessed.
Of course, soft links also have disadvantages that hard links do not have: Because the link file contains the path information of the original file, when the original file is moved from one directory to another, the system cannot find it when accessing the link file. Hard links do not have this defect and can be moved as you wish. In addition, it requires the system to allocate additional space to create a new index node and save the path of the original file.

This post is from ARM Technology
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list