Table of contents
1 inode
When we create a file, we can view the inode of the file with the -i option:
The first option is the inode of the file, and the other columns indicate:
- model
- number of hard links
- file owner
- Belonging group
- size
- Last Modified
- file name
ls -l reads information about files stored on disk and displays them:
So what exactly is an inode?
- Block Group : The ext2 file system will be divided into several Block Groups according to the size of the partition . And each Block Group has the same structure.
- Super Block ( Super Block ): stores the structural information of the file system itself. The recorded information mainly includes: the total amount of bolck and inode , the number of unused blocks and inodes , the size of a block and inode , the time of the last mount, the time of the last data writing, and the time of the last inspection of the disk and other related information of the file system. The information of the Super Block is destroyed, it can be said that the entire file system structure is destroyed.
- GDT , Group Descriptor Table : block group descriptor, describing block group attribute information.
- Block Bitmap ( Block Bitmap ): The Block Bitmap records which data block in the Data Block has been occupied and which data block has not been occupied.
- inode bitmap ( inode Bitmap ): Each bit indicates whether an inode is free and available.
- i node node table : store file attributes such as file size, owner, last modification time, etc.
- Data area: store file content.
Notice:
1 Why does Super Block exist in the attribute information of each file in the file system ?
This is to prevent the loss of the structure information of the file system itself due to the damage of the super block due to the damage of the attribute information.
2 How to understand delete files?
The essence of deleting a file is to directly set the bit position of the corresponding file in the inode Bitmap to 0, without modifying the content of the data area.
3 What is the difference between a file name and an inode?
The Linux system only recognizes the inode number, the file name does not exist in the inode attribute of the file, and the file name is only for the user to see. We create multiple files in a directory. In essence, the directory maintains the relationship between the file name and the inode, so we can find the file by the file name.
4 Now that the size of each data block has been determined, how should the file be managed if it is too large?
The actual inode will help us manage files by maintaining a table. We use this table to create an index to indicate which data blocks belong to which file, that is, a file may correspond to multiple data blocks, so that even large files We can still manage well.
5 Is it possible that the file data block is not used up, but the inode is gone? Or the inode is still there, but the data block is gone?
Both scenarios are possible. The data block is not used up, but the inode is gone, it may be that a lot of empty files (small files) have been created; the inode is still there, but the data block is gone, and the file created may be too large.
Store attributes and data separately:
1. Storage attributesThe kernel first finds an idle inode (here is 263466 ), and the kernel records the file information into it.2. Store dataThe file needs to be stored in three disk blocks, and the kernel found three free blocks: 300, 500 , 800 . Copy the first block of data in the kernel buffer to 300 , the next block to 500 , and so on.3. Record assignmentsThe file content is stored in order 300, 500, 800 . The above-mentioned block list is recorded in the disk distribution area of the kernel on the inode .4. Add the file name to the directoryThe new file name is abc . How does linux record this file in the current directory? The kernel adds entries ( 263466 , abc ) to the catalog file. The correspondence between the file name and the inode connects the file name with the contents and attributes of the file.
2 soft links
Let's first look at what the basic command is?
ln -s 源文件名字 要生成的软链接文件名字 //可以指定路径生成
I don’t know if you have noticed that the inode number of the file generated through the soft link is different from that of the source file, that is to say, the generated soft link file and the source file are two files, so what is the function of the soft link?
Let's change the application scenario:
We create a series of directories and create a file in the last directory, resulting in this structure:
When we want to run the executable file in the same directory as the a directory, we can use this method:
This method is a soft link. Through the soft link, we can directly run the executable file in the way of ./test-sortt, and no longer need to specify the path to search one by one. This method is similar to the desktop shortcut in Windows.
3 hard links
Corresponding to soft links, the commands for hard links are as follows:
ln 源文件名字 要生成的硬链接文件名字
Through the hard link, it is not difficult to find that the inode number of the generated hard link file is exactly the same as that of the source file, indicating that no new file is generated at this time, but an alias is given to the source file. We observe the data in the third column It can also be known that the data in the third column of the above figure represents the number of hard links.
If we want to remove hard links, we can use unlink in addition to rm (soft links can also be deleted with this command).
The default number of hard links we create for an ordinary file is 1, so what is the default number of hard links we create for a directory?
Let's try:
Why is it 2?
Let's observe the following picture:
At this time, because there are two hidden directories under the directory we created by default . and .. where. represents the current path, . shares an inode with dir, and .. is the inode of the previous path (lesson14) of the dir path, So this also explains why the default number of hard links to create a directory is 2.
Here comes the question, can we create hard links for directories?
The answer is no, that is, the operating system does not allow users to create hard links for directories, why?
In fact, it is also easy to explain. If the operating system allows users to create hard links to directories, then the tree structure of the directory may be destroyed and become a graphical structure, so there will be problems when searching for files or directories.
When explaining the basic commands of linux before, the three times of the file have been proposed:
- Access last access time
- Modify file content last modification time
- Change attribute last modification time
The last two are relatively easy to understand. The key is the first Access. In fact, I have already mentioned it when explaining the makefile. When we do not modify the content of the file, the last access time of the file will not change, that is, the last time make The time is earlier than the time when the file content was modified, so you will not be allowed to make again.