The zero-based Linux 21st Chapter
In the tense study, and ushered in a new week ~ ~ Small in the students urged to continue to update the blog.
Students: "We did not ask you!" ”
Alas?? Students do not cooperate at all ~ ~ Don't be embarrassed.
OK ~ Don't tease you, small make up to save everyone's time, quickly start this chapter of the course Bar ~ ~
In the previous chapter, the small series to the students to tell the inode number and Block, but not to the students to finish Oh ~ So the title of the previous chapter is one, then undoubtedly this chapter is the second content of the ~ From the previous chapter, students have learned what is inode and block, and what is included in the Inode, forget the students can look at a chapter of the content ~ ~ and, when the inode number under a partition is exhausted, even if there is more space remaining will not be able to create a new file, in the last chapter, Students also learned that Linux in a directory of the Bolck really stored in the data is what, and left some problems, this chapter, small series to take the students to solve the problem it ~ ~
We know that each partition has an inode number on the filesystem and a block, and this inode is unique under the same partition, used to identify a file, the inode number may be the same in different partitions, but because it is in different partitions, so there is no conflict, so we are copying and moving, What is the operating system doing when deleting files?
In fact, there are two cases when copying and moving a file: The first is under the same partition ; The second type is under different partitions . In both cases, there are two cases: the target exists and the target does not exist
Small series to introduce the situation:
CP copies a file under the same partition: (if the target does not exist)
When we use CP to copy a file, if the target is in the same directory as the source, the same inode number can only point to the same block, because if the same inode number points to a different block, when the system uses this inode number to obtain the data it points to, chaos occurs. Note that the same block here is the same data, for example, small series now to write to a file, a block storage data is limited, and when the content of the file is written too much, will use more than block, but these blocks belong to the same data, belong to the same file content. Therefore, if an inode number points to a different block, it means that an inode number points to a different file, then when you want to use this inode query data, the results can be imagined ... Therefore, it is not difficult to imagine that when copying a file to the same partition, the system will assign the copied target file an inode number, the same will copy the source file block.
CP copies a file under the same partition: (if the target exists)
when the CP copies the file, if the target exists, it will prompt us whether to overwrite the file, if we choose overwrite, it will overwrite the original data of this target, but the inode number of this file will be preserved, but the metadata of this file will be overwritten.
CP copies a file under different partitions: (if the target does not exist)
Since each partition has its own inode number under different partitions, this small part does not explain too much, it will copy the source file data (block) and metadata, and create a new file in the directory where the target is located to assign a new inode number.
CP copies a file under different partitions: (if the target exists)
As in the same partition under the same situation, the small part here does not do too much introduction ~ ~
The above is the case of CP copy file, below, the small part to introduce the move file:
MV moves a file under the same partition: (if the target does not exist)
It is also learned from the previous chapter that the contents of the directory are the corresponding relationship between the inode number of all the files in the directory and the file name, and that the inode number is unique under the same partition, so when moving the file, since it is moving and under the same partition, will the inode number remain unchanged? Students can imagine, small series in the same partition to move the file, first assume that the file's inode number will change, then it means that the file will be removed and then created, and the directory of the log inode number and file name corresponding relationship is also deleted, and then assign a new inode to the target. In fact, this step is cumbersome, some of the steps can be completely omitted, that is, under the same partition, since the use of the same partition inode, it is better to simply keep the original file inode number and metadata, Simply delete the file's inode number and file name in the directory where the file resides, and add the record to the directory where the target is being moved. Because the file itself is not sent for migration, even moving very large data can be done instantaneously.
MV moves a file under the same partition: (in the case of the target presence)
If the target exists, it will also prompt us whether to overwrite the file, which is different from the CP command, although the file is overwritten, but the source file also has an inode number, and the inode number of the source file will overwrite the target inode number, fully use the source file Inode, It also deletes the inode number and file name in the same directory as the source file, and deletes the target record in the directory directory, adding the Inode and file name records of the source file.
MV moves a file under different partitions: (if the target does not exist)
If you are not on the same partition, moving a file is equivalent to a collection of two commands: CP copy and RM Delete. Copies the source file's data and metadata to the destination, deletes the source file, assigns it a new inode number under the new partition
MV moves a file under different partitions: (in the case of the target presence)
If the target exists in different partitions, it will also prompt us whether to overwrite, but this is under different partitions, since the source file to allocate an inode, it is better to simply use the target inode, so if not in the same partition and the target exists, then the target inode number will be used, and overwrites its metadata and data.
When the above MV moves the file, inode number and block changes, then, the small part in the RM delete file, first to the students to introduce, what is soft link, what is hard link:
Soft Links/Symbolic Links:
Soft links, also known as symbolic links, are equivalent to "shortcuts" in Windows, and creating soft links to a file is equivalent to creating a shortcut, which is small, so soft links are the same. This soft link store content is the path of the source file, so it will be very small, then create a shortcut will generate a file, create a soft link will also be reborn as a file, so the soft link is assigned an inode number, after a soft link to a file, you can access the soft link to access the source file. No matter in the same partition, a new file is created to allocate an inode, so soft links can be cross-partitioned.
Ln-s: Creating a soft link
Small to/app/directory under the zzz/directory in the/tmp directory created a soft link, you can clearly see the link to the directory/app/zzz, now, small in the/app/zzz directory to create a file, when the small series access to the/TMP/ZZZ
You can see that the contents of the/app/zzz/directory are accessed. Soft links can not only create a directory, but also create a soft link to the file
At this time to access the soft link, the access to the file, through the two files, the size of the soft link is different, because the size of the soft link file depends on the length of the path of the source file, and this path can be a relative path, it can be an absolute path, the absolute path is relatively simple, Just use the absolute path of the source file, then the relative path, the relative path, relative to the link file, rather than the current directory, for example, the small part is now in the/app/directory, to the ZZZ directory in the/tmp directory to create a soft link, small series use a relative path
Small series now in the current directory, using a relative path to create a soft link, then, small series into the/tmp directory to see
Alas?? Soft links show Red, and the path is wrong AH ~ ~ that is because, standing in this soft link angle to consider, it can not find relative to its path relative path under the ZZZ directory, small series in the same way to create a soft link under the/app/directory
At this point the soft link is available because it can find the source file it points to.
After the soft link link to the source file is deleted, the soft link will not be available, this small part does not show ~ ~
So, through a soft link, we also lead to the fact that a file in the Inode pointer, not only point to the block, but also may point to another pointer, and another pointer can also point to another pointer, by the other pointers to Blck
Ah ~ ~ Small knitting Good tired ~ ~ ... It's better to get here first ~ ~ About hard links, and delete files when the operation, and so on the next chapter of the small part of the students to fill up ~ ~
The zero-based Linux 21st chapter (INODE and block details of the second and soft links)