Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

WBOY
Release: 2022-02-21 17:50:01
forward
4216 people have browsed it

This article brings you relevant knowledge about hard links and soft links in Linux, as well as inode-related issues. I hope it will be helpful to everyone.

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

Preface

The front-end package manager pnpm has been really popular recently, and a large number of articles have analyzed the principles of pnpm. After understanding it, I found that the entire architecture of pnpm is organized based on hard links and soft links, but I am vague about these two concepts, so I want to study it.

As we all know, everything in Unix/Linux systems is a file. It can be seen that files are very important in Linux systems. Our usually more intuitive feelings about files are definitely the file name and file content. But in the Linux file system, in addition to file names and file contents, there is also a very important concept, which is inode.

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

inode

Wikipedia describes inode like this:

The inode (index node) is a data structure in a Unix -style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attributes may include metadata (times of last change, access, modification ), as well as owner and permission data.

A directory is a list of inodes with their assigned names. The list includes an entry for itself, its parent, and each of its children.

means: inode is a data structure used to describe file system objects (such as files or folders) in Unix-like file systems. It stores various attributes of the file (meta-information such as the time of the last inode change, the time of the last access, the time of the last modification, and permission information, etc.). A folder is a group of inodes, including its own entry, the entry of its parent node, and all child nodes.

In fact, inode contains more than the above, specifically:

  • The number of bytes of the file

  • The number of bytes of the file User ID

  • Group ID of the file

  • Read, write, and execute permissions of the file

  • Timestamp: ctime, the time when the inode was last changed; mtime, the time when the file content was last changed; atime, the time when the file was last opened

  • Number of links, that is, how many files there are The name points to the location of this inode

  • file data block

In the ext2/ext3 file system used by Linux, different types of data are stored in different Area. The inode table composed of inodes is stored in one location, and the file data blocks are stored in another location.

inode does not contain the file name, the file name is stored in the folder information structure. The file name is equivalent to the alias of the inode, which is convenient for us to manage and remember. The Linux system operates on files through inodes. When we modify a file, the system finds the inode corresponding to the file name from the information structure of the folder, and then finds the corresponding inode through the file data block address stored in the inode. Read and write operations are performed on the hard disk location.

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

Hard link

Generally speaking, inode has a one-to-one relationship with file name and file data, but we can use shell commands to make multiple The file names point to the same inode, which is a hard link.

Use the ln command to create a hard link, such as

ln test.txt test_hard.txt
Copy after login

corresponding to the fs.link method of nodejs.

Before creating a hard link, test.txt can be represented as follows:

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

After creating a hard link:

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

You can see that the inode of test_hard.txt is the same as the source file test.txt, but now the number of links has become 2.

We can execute ls -li to check it out.

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

The first column is the inode number, you can see that both are 13029546, so the two files use the same inode. The second column is permission information, the fourth column is the owner, and the sixth column is the file content size. As you can see, except for the different file name, the file created by the hard link has exactly the same meta information as the source file. The third column indicates the number of links. As you can see, the current number of links is 2.

Since the hard link file and the source file use the same inode and point to the same block of file data, all information except the file name is the same. Therefore, these two files are equivalent and can be said to be hard link files to each other. Modify any file and you can see that the contents of the other file will also change simultaneously.

软链接

准确来说叫符号链接(symbolic link),一般又叫软链接(soft link)。与硬链接共用一个inode不同,软链接会创建新的inode,并指向源文件。可以理解软链接就是windows系统中的桌面快捷方式。

创建软链接的命令和硬链接很像,多了-s参数:ln -s

ln -s test.txt test_symbolic.txt
Copy after login

对应的nodejs的fs.symlink方法。

创建软链接之后:

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

源文件inode的链接数还是1,创建了新的inode,软链接指向源文件。

执行ls -li看一下:

Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

可以看到,软链接的inode number跟源文件的不一样,权限一列开头为小写L,表示软链,链接数为1,大小为8个字节。没错,软链文件也有大小,不过一般很小,毕竟只是一个快捷方式。

对比

文件重命名或文件移动

文件重命名和文件移动对于Linux系统来说都是文件绝对路径的更改。对硬链接来说,文件重命名或文件移动不会改变链接指向,而对软链接来说,文件重命名或文件移动则使链接断开,这时通过软链接修改文件内容时会重新创建一个新的inode,跟原文件名和文件数据块关联。

文件删除

rm命令或者nodejs的unlink其实是将inode的链接数减1。对于前文的硬链接,删除test_hard.txt使得inode1的链接数变成1,当链接数变成0时,系统就会释放掉这个inode,之后再创建的新文件就可以使用该inode的inode number了。这时没有inode指向文件数据block,所以文件找不到了。但实际上文件数据还存在硬盘中,所以经常能看到网上有一些帮助恢复误删的文件的工具。软链接inode链接数为1,删除软链接则系统释放该inode。

链接文件和文件夹

软链接可以链接文件和文件夹,但硬链接只能链接文件。

不同文件系统创建链接

软链接可以跨不同的文件系统创建,但是硬链接不行,因为硬链接是共用一个inode,而不同的文件系统有不同的inode table。

应用场景

硬链接

  • 文件备份:为了防止重要的文件被误删,文件备份是一种好的办法,但拷贝文件会带来磁盘空间的消耗。硬链接能不占用磁盘空间实现文件备份。

  • 文件共享:多人共同维护同一份文件时,可以通过硬链接的方式,在私人目录里创建硬链接,每个人的修改都能同步到源文件,但又避免某个人误删就丢掉了文件的问题。

  • 文件分类:不同的文件资源需要分类,比如某个电影即是的分类是外国、悬疑,那我们可以在外国的文件夹和悬疑的文件夹里分别创建硬链接,这样可以避免重复拷贝电影浪费磁盘空间。有人可能说,使用软链接不也可以吗?是的,但不太好。因为一旦源文件移动位置或者重命名,软链接就失效了。

软链接

  • 快捷方式:对于路径很深的文件,查找起来不太方便。利用软链接在桌面创建快捷方式,可以迅速打开并编辑文件。

  • 灵活切换程序版本:对于机器上同时存在多个版本的程序,可以通过更改软链接的指向,从而迅速切换程序版本。这里提到了python版本的切换可以这么做。

  • 动态库版本管理:不是很懂,具体可以看这里。

总结

Linux系统通过inode管理文件,inode存储着文件字节数、文件权限、链接数、数据block位置等信息。

硬链接与源文件共用inode,除了文件名不同,其他与源文件一样。不能对文件夹创建硬链接,不能对不同的文件系统的文件创建硬链接。

软链接类似于windows的快捷方式,有独立的inode。可以对文件夹或不同文件系统的文件创建软链接。

硬链接和软链接修改文件内容都会同步到源文件,因为本质上它们都是指向源文件的数据block。

相关推荐:《Linux视频教程

The above is the detailed content of Let you understand Linux hard links and soft links (detailed explanation with pictures and texts). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template