Inode, hard link, and soft link methods for Linux File storage, and inode for file storage

Source: Internet
Author: User

Inode, hard link, and soft link methods for Linux File storage, and inode for file storage
File Storage

Files are stored on hard disks,Slice(Sector) is the smallest physical storage unit in the disk. Generally, the size of each slice is 512 bytes, but to improve efficiency, when reading a hard disk, the operating system often reads multiple sectors at a time (multiple sectors constitute a block ).Block(Block) is the minimum unit for file access; the most common block size is 4 kb;

Because files are stored in blocks, we need some special items (inode) to index these blocks and also contain some metadata of the files, for example, the Creator, creation date, and creation time. These special things are called "index nodes" (inode). You can use the command: stat file name to view inode information of a file;

Inode: 1036334 indicates the inode number corresponding to the file. Other information is described below;

Because inode also needs to be stored and consumes disk space, when the disk is formatted, the operating system divides it into two parts, one for storing inode and the other for storing real data; because inode is set when formatting a disk, and the number of inode is limited, it is possible that the inode is used up and the disk still has space, but files cannot be created. You can run the following command: df-I: view the total number of inodes and the number of inodes used in each hard disk partition;

In fact, the operating system does not open a file by file name, But index the corresponding block through inode to find the target file. How does the operating system find these inode? Each inode has a corresponding ID. We open a file, which is divided into three steps in the system: (1) the system finds the inode number corresponding to the file name; (2) finds the corresponding inode through the inode number and obtains the inode information, determine permissions, etc.; (3) index the corresponding block through inode to read data; you can view the inode number of a file by using the command: ls-I file name;

The relationships between hard links, soft links, inode, and block are as follows (soft and hard links are described below ):

Hard link and soft link

Generally, file names correspond to inode numbers, but Linux allows multiple file names to point to the same inode number. For example, we create file file1 and create file2 through hard links, view the inode numbers of the two files respectively and you will find that they are the same. What is a hard link? What is the corresponding soft link?

Hard link:

You can use different file names to access the same content (or the same inode) and modify the file content, it will be reflected in all file names (this is more like a reference to pass a value in php), but deleting a file name does not affect other file names, the file content will not be deleted. Only when all file names are deleted will the file content be deleted. This is called a hard link, multiple File Names Index the same inode number. You can use the stat file name to view the number of file name links corresponding to the inode of a file. For example:

Links: 2 indicates that there are two file names linked to the inode (1036334). This is because I created the file file1, at the same time, a hard link file2 of the file is created (specific operations are described below );

Examples of creating a hard link are as follows:

New File file1: touch file1

Create a hard link file2: ln file1 file2

View File Information: ls-li

The inode numbers corresponding to the two file names are 1036334, and the number of links to the inode is 2 (-rw-r-permission after the number indicates the number of links to the inode number)

Delete file1: sudo rm file1

View File Information: ls-li

The file still exists, and the inode number is not changed. The number of links to the inode becomes 1;

Soft link:

The soft link (file3 in the following text) is actually more like the desktop shortcut in windows. It is also a file (with a new inode number ), it only saves not the file content (file1), but the address of the file name pointed to by the soft link (file1 address). If you delete file3, it will not affect the file content. Delete file1, invalidates file3;

Create soft link file3: ln-s file1 file3

View File: ls-li

The number of links linked to 1036334inode is still 2, indicating that file3 is not linked to 1036334

Delete file3: sudo rm file3

View File: ls-li

Does not affect file1;

Delete file1: sudo rm file1

View File: ls-li

It is found that file3 has become red and invalid;

Related Article

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.