Table of Contents
Preface
inode
Hard link
软链接
对比
文件重命名或文件移动
文件删除
链接文件和文件夹
不同文件系统创建链接
应用场景
硬链接
总结
Home Operation and Maintenance Linux Operation and Maintenance Let you understand Linux hard links and soft links (detailed explanation with pictures and texts)

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

Feb 21, 2022 pm 05:50 PM
linux

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
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!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is Linux actually good for? What is Linux actually good for? Apr 12, 2025 am 12:20 AM

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

How to start monitoring of oracle How to start monitoring of oracle Apr 12, 2025 am 06:00 AM

The steps to start an Oracle listener are as follows: Check the listener status (using the lsnrctl status command) For Windows, start the "TNS Listener" service in Oracle Services Manager For Linux and Unix, use the lsnrctl start command to start the listener run the lsnrctl status command to verify that the listener is started

How to set up a recycling bin in Debian system How to set up a recycling bin in Debian system Apr 12, 2025 pm 10:51 PM

This article introduces two methods of configuring a recycling bin in a Debian system: a graphical interface and a command line. Method 1: Use the Nautilus graphical interface to open the file manager: Find and start the Nautilus file manager (usually called "File") in the desktop or application menu. Find the Recycle Bin: Look for the Recycle Bin folder in the left navigation bar. If it is not found, try clicking "Other Location" or "Computer" to search. Configure Recycle Bin properties: Right-click "Recycle Bin" and select "Properties". In the Properties window, you can adjust the following settings: Maximum Size: Limit the disk space available in the Recycle Bin. Retention time: Set the preservation before the file is automatically deleted in the recycling bin

How to restart the apache server How to restart the apache server Apr 13, 2025 pm 01:12 PM

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

See all articles