search
HomeOperation and MaintenanceLinux Operation and MaintenanceLet 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.

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)

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

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

对应的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!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools