Most of Linux file system ( Such as ext2、ext3) Regulations , A file consists of directory entries 、inode And data blocks :

Catalog items : Include the file name and inode Node number .

 Inode: Also called file index node , Contains the basic information of the file and the pointer to the data block .

  Data blocks : Contains the specific content of the file .

First say inode

understand inode, Start with file storage . The files are stored on the hard disk , The smallest unit of hard disk storage is called " A sector "(Sector), Each sector stores 512 byte ( amount to 0.5KB).

When the operating system reads the hard disk , It doesn't read sector by sector , That's inefficient , Instead, read multiple sectors continuously at once , Read one at a time " block "(block). This is made up of multiple sectors " block ", Is the smallest unit of file access ." block " Size , The most common is 4KB, Eight in a row sector Form a block.

File data is stored in " block " in , So obviously , We also have to find a place to store the meta information of the file , For example, the creator of the file 、 Date the file was created 、 File size and so on . This area of file meta information is called inode, The Chinese translation is " The index node ".

inode Contains the meta information of the file , Specifically, there are the following contents :

  The number of bytes in the file .

  Of the file owner User ID.

  Of documents Group ID.

  The file to read 、 Write 、 Executive authority .

  The time stamp of the file , There are three :ctime finger inode Time of last change ,mtime It refers to the time when the content of the document was last changed ,atime The last time the file was opened .

  Link number , How many file names point to this inode.

  File data block The location of .

It can be used stat command , Check the... Of a file inode Information :stat demo.txt

All in all , All file information except file name , All exist inode In . As for why there is no filename , There will be a detailed explanation below .

When viewing a file , We'll start with inode Find out the file properties and data storage points in the table , Then read the data from the data block .

Please see the file storage structure diagram :



inode Size

inode It also consumes hard disk space , So when the hard disk is formatted , The operating system automatically divides the hard disk into two areas . One is the data area , Store file data ; The other is inode District (inode table), Deposit inode The information contained .

Every inode The size of the node , It's usually 128 Byte or 256 byte .inode The total number of nodes , Given when formatting , Generally, every 1KB Or every 2KB Just set up a inode. Suppose it's in one piece 1GB In the , Every inode The size of the node is 128 byte , Every time 1KB Just set up a inode, that inode table The size of will reach 128MB, Take up the whole hard disk 12.8%.

Check... For each hard disk partition inode The total number and the number used , have access to df -i command .

Look at each inode The size of the node , You can use the following command : sudo dumpe2fs -h /dev/hda | grep "Inode size"

Because every file must have a inode, So it's possible that inode It's used up , But the hard disk is not full . At this time , You can't create new files on your hard disk .

inode number

Every inode All have a number , For the operating system inode Number to identify different documents .

It's worth repeating ,Linux No file name is used inside the system , While using inode Number to identify the file . For the system , The filename is just inode A nickname or nickname that is easily recognizable . On the surface, , The user passes the filename , Open file . actually , This process in the system is divided into three steps : First , The system finds the file name corresponding to inode number ; secondly , adopt inode number , obtain inode Information ; Last , according to inode Information , Find the file data block, Read the data .

Use ls -i command , You can see that the file name corresponds to inode number , for example : ls -i demo.txt

Catalog items

Linux In the system , Catalog (directory) It's also a kind of document . Open Directory , It's actually opening a directory file .

The structure of the catalog file is very simple , It's a series of catalog items (dirent) A list of . Each catalog item , It's made up of two parts : The filename of the included file , And the file name inode number .

ls The command lists only all filenames in the directory file : ls /etc

ls -i Command to list the entire catalog file , That is, the filename and inode number : ls -i /etc

If you want to see the details of the file , It must be based on inode number , visit inode node , Read information .ls -l Command to list file details . ls -l /etc

Hard and soft links

Hard links

In general , File name and inode The number is " One-to-one correspondence " Relationship , Every inode The number corresponds to a file name . however ,Linux The system allows , Multiple filenames point to the same inode number . It means , You can access the same content with different file names ; Modify the contents of the document , Will affect all filenames ; however , Delete a filename , Does not affect access to another filename . This situation is called " Hard links "(hard link).

ln Command to create a hard link , The grammar is : ln source_file target_file

After running the above command , Source and destination files inode Same number , All point to the same inode.inode One of the messages is called " Link number ", The record points to the inode The total number of filenames for , It will increase 1. In turn, , Delete a filename , Would make inode Nodes in the " Link number " reduce 1. When this value is reduced to 0, Indicates that no filename points to this inode, The system will recycle this inode number , And the corresponding block Area .

By the way, here's the catalog file " Link number ". When creating a directory , Two catalog entries are generated by default :"." and "..". formerly inode The number is the inode number , Equivalent to the current directory " Hard links "; Latter inode The number is the parent directory of the current directory inode number , Equivalent to the parent directory " Hard links ". therefore , Of any directory " Hard links " total , Always equal to 2 Plus the total number of subdirectories ( Including hidden Directory ), there 2 It's the parent directory to which “ Hard links ” And in the current directory ". Hard links “.

Soft link

Besides hard links , There is another special case . file A And documents B Of inode The numbers are different , But the documents A It's about documents B The path of . Read the file A when , The system will automatically direct visitors to files B. therefore , No matter which file you open , The final read is all the files B. At this time , file A It's called a document B Of " Soft link "(soft link) perhaps " A symbolic link (symbolic link).

It means , file A Depends on files B And exist , If the file is deleted B, Open file A You're going to report a mistake :"No such file or directory". This is the biggest difference between soft links and hard links : file A Point to file B The name of the file , Not the file B Of inode number , file B Of inode" Link number " It won't change .

ln -s Command to create a soft link , The grammar is : ln source_file target_file

【Linux】 More articles on file storage structure

  1. linux Storage structure of

    stay linux The storage structure is as follows : Linux The file storage structure in the system So in linux What's the meaning of each directory in the stay linux Relative path and absolute path are concepts that must be understood Absolute path (absolute path): ...

  2. Intercept linux File storage path method

    1. Intercept linux File storage path method package com.tydic.eshop.action.freemarker; public class dddd { public static void ...

  3. PostgreSQL Storage system 2 :REDOLOG File storage structure 2

    REDOLOG The user data in the file has the same storage structure as the user data in the data file A few months ago, my colleague gave training to a company in Taiwan <pg9 ad admin> when , One student mentioned WAL What's recorded in is Query At the time of the SQL sentence ( Than ...

  4. Docker Image file storage structure

    docker The relevant documents are stored in :/var/lib/docker Under the table of contents The storage structure of image is mainly divided into two parts , One is the mirror image ID Relationship between , One is the mirror image ID Association with the image name , The structure of the former is called Graph, The latter is called TagStore ...

  5. Linux File system directory structure

    At the beginning of our study embedded Linux when , The first thing to learn is Linux The smallest root file system of : Now I will elaborate for beginners Linux The smallest root file system of . The root directory is Linux Middle is “/”, To go to the root , command “cd  /” namely ...

  6. Innodb、MYISAM File storage structure of

    MySQL Each database of is stored in a folder with the same name as the database ,MySQL Database files include MySQLserver The created database file and MySQL Database files created by the storage engine used . see MySql The database physical files are stored in ...

  7. Linux File system directory structure

    Get into Linux root directory ( namely "/",Linux Entry to the file system , It's also a directory at the highest level ), function "ls -l" command , You can see Linux System catalog . 1./b ...

  8. 【linux knowledge 】 File storage structure and physical device naming (FHS)

    FHS It's a file system hierarchy standard (Filesystem Hierarchy Standard). Everything from the root directory " / " Start . Linux Common contents and corresponding contents in : Paths are divided into : Absolute path ( ...

  9. File storage structure inode And RAM Structure makes connections

    linux Everything below is a document , It can be roughly divided into the following categories : Catalog . Ordinary documents . Hard to connect . Soft connection . Character device . Block device .FIFO.Socket, It is stored in the physical storage body inode And block storage ,inode For metadata , In addition to the actual data ...

Random recommendation

  1. Apworks Framework practice

    Apworks Framework practice ( One ):Apworks What is it ? Apworks Framework practice ( Two ): Start using Apworks Framework practice ( 3、 ... and ): Unit testing and continuous integration Apworks Framework practice ( Four ): Use Visual St ...

  2. java Realize binary tree

    The stack cannot be used when traversing for loop , Because of his size The function gets smaller dynamically .

  3. WPF according to Oracle Table of database , Generate CS File widget

    The reason for developing gadgets : 1. Our company's development is for clients C#, The service side with Java, When the front and back platforms communicate with each other , use Oracle Database fields , Because the server side has a basis developed by the general manager of the company Oracle Database table generated cla ...

  4. C# Revision in China Dll file ( Recompile after decompile )

    Dll After file generation , Without source code , And we have to change the content   You can use Microsoft's own ildasm and ilasm Program   First use ildasm take dll Decompile the file to il file  ildasm Test.dll /  ...

  5. file I/O( Without buffer ) And I/O The efficiency of

    Program listing 3-3 Using the program in read and write Function to copy files . The following points should be noted about the procedure : It reads from standard input , Write to standard output , This assumes that before performing this procedure , These standard inputs . The output has been shell Arrange it . exactly , All the usual ...

  6. hdu 5076 The minimum cut can be used flexibly

    That means more complex problems , The key facts are abstracted : Each point , Can give an existing value ( Pick two and one . Need to choose , Set up ai,bi). Look for all and the biggest . Conditional : If two points satisfy at the same time : 1,: Binary just has one difference .  2: Medium ...

  7. JavaEE(14) - JPA Association mapping

    1. A one-way N-1 Association mapping 2. A one-way 1-1 Association mapping 3. A one-way 1-N Association mapping 4. A one-way N-N Association mapping 5. two-way 1-1 Association mapping 6. two-way 1-N Association mapping 7. two-way N-N Association mapping 1. A one-way N-1 Turn off ...

  8. vector utilize swap Function to release memory vector&lt;int&gt;().swap

    First ,vector And deque Different , Its memory footprint will only grow , It doesn't decrease . For example, you first assigned 10,000 Bytes , then erase Drop the back 9,999 individual , Even though there is only one valid element , But the memory footprint is still 10,000 individual . all ...

  9. Mac Let's build it quickly PHP development environment

    Recently, I did a back-end project , Yes, it is PHP+MySQL+Nginx It's done , So let's summarize the methods of building environment . remarks : materiel :Apache/Nginx+PHP+MySQL+MAMP Mac OS 10.12.1 ...

  10. Found one that's better than echarts Better B, A more dazzling chart component d3.js

    Learn a little every day Programming PDF e-book . Video tutorial free download : d3.js  , Can make more complex charts ...