Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Inode in Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Linux inodeinode translated into Chinese is the index node. Each storage device or partition of a storage device (storage device is hard disk, floppy disk, USB flash drive... ... After formatting as a file system, there should be two parts, one part is inode, the other part is Block, Block is used to store data. The inode is the information used to store these data, including file size, ownership, user group, read and write permissions, etc. The inode indexes information for each file, so there is a value for the inode. According to the instructions, the operating system can find the corresponding file most quickly through the inode value.

For example, a book, storage device or partition is equivalent to the book, Block is equivalent to each page in the book, inode is equivalent to the directory in front of the book, a book has a lot of content, if you want to find a part of the content, we can first look up the directory, through the directory can find the content we want to see the fastest. Although it was inappropriate, it was still more vivid.

When we use ls to view a directory or file, if we add the-i parameter, we can see the inode node; for example, we mentioned the example above;

[root@localhost ~]# ls -li lsfile.sh

2408949 -rwxr-xr-x 1 root root 7 04-21 12:47 lsfile.sh

The inode value for lsfile.sh is 2408949 ; to view the inode of a file or directory, use the-i argument of the ls command.

inode The same file is a hard-linked file;

In the Linux file system, files with the same inode value are hard-linked files, that is, different file names, inode may be the same, one inode value can correspond to multiple files. It's not hard to understand link files, just look at examples. In Linux, link files are created using the ln tool.

Create hard links, hard links and source file relationships;

Syntax for creating file hard links with ln:

# ln source file destination file

Let's take an example where we want to create its hard link sun002.txt for sun.txt. Then take a look at the changes in the attributes of sun.txt and sun002.txt;

[root@localhost ~]# ls -li sun.txt Note: View sun.txt properties;

2408263 -rw-r--r-- 1 root root 29 04-22 21:02 sun.txt Note: This is an attribute of sun.txt;

[root@localhost ~]# ln sun.txt sun002.txt Note: We create the hard link file sun002.txt of sun.txt by ln

[root@localhost ~]# ls -li sun* Note: Let's list sun.txt and sun002.txt

2408263 -rw-r--r-- 2 root root 29 04-22 21:02 sun002.txt

2408263 -rw-r--r-- 2 root root 29 04-22 21:02 sun.txt

We can see that sun.txt has a link count of 1 (i.e., the number after-rw-r--r--) when the hard link file sun002.txt is not created, and this value becomes 2 when the hard link sun002.txt is created. That is, every time we create a new hardlink file for sun.txt, the number of hardlinks increases by 1.

Files with the same inode value are hard-linked to each other. When we modify the contents of one of these files, the contents of the hard-linked files change as well. If we delete a file that is hard-linked to each other, the other files are not affected. For example, after we delete sun.txt, we can still see the contents of sun002.txt, and sun02.txt still exists.

It can be understood that files that are hard linked to each other seem to be clones, and their attributes are almost exactly the same;

In the example below, we delete sun.txt and see if sun002.txt can see its contents.

[root@localhost ~]# rm -rf sun.txt

[root@localhost ~]# more sun002.txt

Note: Hard links cannot be created for directories, only files.

The creation of soft links, and soft links and the relationship between source files;

Syntax for creating soft links (also known as symbolic links);

# ln -s source file or directory destination file or directory

Soft links are also called symbolic links. They differ from hard links in that soft link files are just a marker of their source files. When we delete the source file, the link file does not exist independently, although the file name is still retained, but we can not view the contents of the soft link file.

[root@localhost ~]# ls -li linuxsir001.txt

2408274 -rw-r--r-- 1 root root 29 04-22 21:53 linuxsir001.txt

[root@localhost ~]# ln -s linuxsir001.txt linuxsir002.txt

[root@localhost ~]# ls -li linuxsir001.txt linuxsir002.txt

2408274 -rw-r--r-- 1 root root 29 04-22 21:53 linuxsir001.txt

2408795 lrwxrwxrwx 1 root root 15 04-22 21:54 linuxsir002.txt -> linuxsir001.txt

explain

For the above example, first we look at the properties of linuxsir001.txt, such as inode, file type, creation or modification time, etc... ... Let's compare:

First compare the nodes: the nodes of the two files are different;

Second, the two files belong to different types linuxsir001.txt is-, that is, ordinary files, while linuxsir002.txt is l, it is a link file;

The third two files have different read and write permissions linuxsir001.txt is rw-r--r--, while linuxsir002.txt has read and write permissions rwxrwxrwx

Third, both have the same number of hard links; both are 1.

the owner of the fourth two files is the same as the user group to which they belong;

Fifth, the modification (or access, creation) time is different;

We also noticed that linuxsir002.txt is followed by a tag->, which indicates that linuxsir002.txt is a soft link file to linuxsir001.txt.

It is worth noting that when we modify the contents of a linked file, we are modifying the contents of the source file. Of course, the attributes of the source file will also change, and the attributes of the linked file will not change. When we delete the source file, there is only one file name for the link file, because the source file is lost, so the soft link file does not exist. This is different from hard linking;

[root@localhost ~]# rm -rf linuxsir001.txt Note: delete linuxsir001.txt

[root@localhost ~]# ls -li linuxsir002.txt Note: View linuxsir002 properties;

2408795 lrwxrwxrwx 1 root root 15 04-22 21:54 linuxsir002.txt -> linuxsir001.txt

[root@localhost ~]# more linuxsir002.txt Note: View the contents of linuxsir002.txt;

linuxsir002.txt: No such file or directory Note: get prompt, linuxsir002.txt does not exist.

The above example tells us that if a linked file loses its source, it means that it no longer exists;

We can see that the soft link file is actually just a mark of the source file, and when the source file is lost, it is also present. Soft link files only occupy inode to store soft link file attributes and other information, but file storage points to the source file.

Software links can be applied for files or directories. Both soft and hard links can be deleted with rm. RM tools are universal.

personal experience

Soft link is another file, the role can be understood as a pointer, ***** action on this file except delete all direct to the actual point to the file, because it is a real file so occupy disk space

Hard link can be considered not a file, it is just an alias of the actual file, its role is to prevent the real file from being misoperated, after a hard link is established for a file, they are aliases for each other, delete any of them,

In this way, RM only deletes the alias, and the actual file is not deleted. The original file is deleted only if the number of links is 0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report