Detailed explanation of inode, hard link and soft link for Linux file storage

1.What is an inode?

        First of all, everything in Linux is a file and everything is a text stream.

        inode, the Chinese translation is "index node", also called "i node"

         Files are stored on the hard disk. The smallest storage unit of the hard disk is called "Sector". Each sector stores 512 bytes (equivalent to 0.5KB).

        When the operating system reads the hard disk, it does not read it sector by sector, which is too inefficient. Instead, it reads multiple sectors continuously at one time, that is, it reads one "block" at a time. This "block" consisting of multiple sectors is the smallest unit of file access. The most common size of "block" is 4KB, that is, eight consecutive sectors form a block.        

        The file name and file data of the file in the Linux system are stored separately, and the file data is divided into actual data and meta information.
Meta information is similar to file attributes, including the creator of the file, creation date, file size, file permissions and other information. The actual information is stored in the block, while The area where file metainformation is stored is called inode, so a file must occupy an inode and at least one block.
        Linux internally uses inode numbers to identify files, rather than file names. For the system, the file name is an alias for the inode number, which is convenient for users to identify files. The file name and inode number are one by one. Corresponding relationship, each inode number corresponds to a file name.
 

2. View file inode information

        Command:stat statement subject 

[test@test ~]$ stat 1.sh 
  File: ‘1.sh’  
  Size: 48        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 67308180    Links: 2
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/    test)   Gid: ( 1000/    test)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2023-10-20 10:24:37.122033787 +0800
Modify: 2023-10-16 11:21:29.118521587 +0800
Change: 2023-10-20 10:58:12.815119852 +0800
 Birth: -

--显示信息包括
--文件,大小,块,io块,文件类型,设备,inode号,链接,权限,用户,组
--最近访问信息,最近修改,最近改变信息,创建信息

        In short, all file information except the file name is stored in the inode. Unix/Linux systems do not use file names internally, but use inode numbers to identify files. For the system, the file name is just an alias or nickname for the inode number for easy identification.

        inode 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, which stores file data; the other is the inode area (inode table), which stores the information contained in the inode.
       View the total number of inodes in each hard disk partition and the number that has been used Command: df -i 

        Since each file must have an inode, it may happen that the inodes are used up but the hard disk is not full. At this time, new files cannot be created on the hard drive.

3. What happens when you open a file

--If it is a new file, the file will assign a unique inode number to the file and associate the inode number with the file name.

1. The system finds the inode number corresponding to the file name;

2. Obtain inode information through inode number;

3. Based on the inode information, find the block where the file data is located and read the data.

4. Hard link and soft link

        The ln (English spelling: link files) command is a very important command. Its function is to establish a synchronized link for a file in another location.

ln [参数][源文件或目录][目标文件或目录]

        Commonly used parameters: Soft link parameter -s. If not added, it is a hard link. I don’t use the other parameters commonly.

  • --backup[=CONTROL] Back up existing target files
  • -b is similar to --backup , but does not accept parameters
  • -d allows superuser to make hard links to directories
  • -f force execution
  • -i interactive mode, if the file exists, the user will be prompted whether to overwrite it.
  • -n treat symbolic links as normal directories
  • -s soft link (symbolic link)
  • -v displays detailed processing

5. The difference between hard links and soft links 

        There is a file 1.sh, create a soft link and a hard link for it.

[test@test ~]$ ll
-rwxrwxrwx. 1 test test 48 Oct 16 11:21 1.sh
[test@test ~]$ cat 1.sh 
#! /bin/bash
echo 111
echo $SHLVL
echo $BASHPID
----创建软连接 -s
[test@test ~]$ ln -s 1.sh 1
----硬链接
[test@test ~]$ ln -v 1.sh 2
‘2’ => ‘1.sh’

        Check their inode numbers respectively ll -i

        You can see that the inode number of hard link 2 is the same as the inode number of source file 1.sh, Soft connection 1 generates a new inode number

       Difference 1:

       Hard link: A file name 2 is created in the file system, which shares the same inode block and data block as the original file 1.sh, so their file size and modification time are also the same.

        Soft link: A new file 1 is generated, which can be understood as a shortcut, pointing to the original file 1.sh or the path of the directory, and does not share inode blocks and data blocks. Therefore their sizes and modification times differ

        

        Difference 2:

         Soft links can cross file systems, hard links can only be created in the same file system; soft links can link to a non-existent file name wwwwww; soft links can link to directories, hard links do not allow links to directories

        Hard link exists in the form of a copy of the file. But it does not occupy actualspace. No matter it is a hard link or a soft link, it will not copy the original file and will only occupy a very small amount of disk space.

       Difference 3: If you delete the original file 1.sh, the soft link will become invalid (the pointed file is gone), but the hard link will not.

        yeah! Seeking Like

Guess you like

Origin blog.csdn.net/qq_63693805/article/details/133942188