


Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)
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.
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.
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.
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
ln test.txt test_hard.txt
corresponding to the fs.link method of nodejs.
Before creating a hard link, test.txt can be represented as follows:
After creating a hard link:
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.
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
对应的nodejs的fs.symlink方法。
创建软链接之后:
源文件inode的链接数还是1,创建了新的inode,软链接指向源文件。
执行ls -li看一下:
可以看到,软链接的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!

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

Configuring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration

The steps to install an SSL certificate on the Debian mail server are as follows: 1. Install the OpenSSL toolkit First, make sure that the OpenSSL toolkit is already installed on your system. If not installed, you can use the following command to install: sudoapt-getupdatesudoapt-getinstallopenssl2. Generate private key and certificate request Next, use OpenSSL to generate a 2048-bit RSA private key and a certificate request (CSR): openss

Configuring a virtual host for mail servers on a Debian system usually involves installing and configuring mail server software (such as Postfix, Exim, etc.) rather than Apache HTTPServer, because Apache is mainly used for web server functions. The following are the basic steps for configuring a mail server virtual host: Install Postfix Mail Server Update System Package: sudoaptupdatesudoaptupgrade Install Postfix: sudoapt

To configure the DNS settings for the Debian mail server, you can follow these steps: Open the network configuration file: Use a text editor (such as vi or nano) to open the network configuration file /etc/network/interfaces. sudonano/etc/network/interfaces Find network interface configuration: Find the network interface to be modified in the configuration file. Normally, the configuration of the Ethernet interface is located in the ifeth0 block.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use