Distinguishing between Linux hard links and soft links

Distinguishing between Linux hard links and soft links

In Linux, there are two types of file connections. One is similar to a Windows shortcut, which allows you to quickly link to the target file (or directory). This is called a soft link, also called a symbolic link. The other is to generate a new file name through the file system's inode connection instead of creating a new file. This is called a hard link, also known as an entity link. Soft links and hard links are two completely different things.

1. Hard link

1.1 Introduction

Generally speaking, there is a one-to-one correspondence between file names and inode numbers, and each inode number corresponds to a file name. However, Unix/Linux systems allow multiple file names to point to the same inode number. This means that the same content can be accessed using different file names; modifying the file content will affect all file names; however, deleting one file name will not affect access to another file name. This situation is called a "hard link".

The ln command can create hard links:

ln <source file> <hard link>

After running the above command, the inode numbers of the source file and the target file are the same, and both point to the same inode. There is an item called "link count" in the inode information, which records the total number of file names pointing to the inode, and it will increase by 1 at this time. Conversely, deleting a file name will reduce the "link count" in the inode node by 1. When this value decreases to 0, indicating that no file name points to this inode, the system will recycle this inode number and its corresponding block area.

By the way, let me talk about the "number of links" of directory files. When creating a directory, two directory entries are generated by default: " and "…". The inode number of the former is the inode number of the current directory, which is equivalent to a "hard link" of the current directory; the inode number of the latter is the inode number of the parent directory of the current directory, which is equivalent to a "hard link" of the parent directory. Therefore, the total number of "hard links" of any directory is always equal to 2 plus the total number of its subdirectories (including hidden directories), where 2 refers to the "hard link" to the directory name itself and the "hard link" to the "." in the current directory.

1.2 Relationship between hard links and inodes

A hard link is essentially an alias for a file that maps to the same inode as the source file. Next, create the source file lvlv.txt and the hard link lvlv_hd.txt in the /etc and /root directories respectively, and check the file properties.

#Create a hard link ln /etc/lvlv.txt /root/lvlv_hd.txt 

#List file attributes ll -i /etc/lvlv.txt
7792474 -rw---x--x 2 b3335 b3335 22 Nov 9 21:05 lvlv.txt
ll -i /root/lvlv_hd.txt
7792474 -rw---x--x 2 b3335 b3335 22 Nov 9 21:05 lvlv_hd.txt

From the above, we can see that the inode of the hard link /etc/lvlv_hd.txt is the same as the source file lvlv.txt, and the other information is exactly the same. The storage diagram is as follows:

The content in the figure shows that the hard link lvlv_hd.txt in the directory /root and the file lvlv.txt in /etc point to the same file content through the same inode. The data entity of the directory records each file name and the inode number of the file. It can be seen that a hard link is essentially an alias for a file.

1.3 Hard Link Notes

Hard links have two limitations:
(1) It cannot cross file systems. A hard link is just an alias for a file, not an independent file. Therefore, it can only be performed on a single file system and cannot cross file systems.

(2) Unable to connect to directory. Because hard links connected to directories may cause the directory's inode and physical block to form a loop. At this time, if the directory is deleted, the directory entity block will become inaccessible to the system, resulting in an isolated directory (no longer accessible from the root directory). If you want to delete orphaned directory inodes and entity blocks, you need to perform marking and cleaning, which is very time-consuming to operate on disk. Soft links do not cause this problem because they do not increase the link count of the target directory.

For example, if a hard link /etc/etc_hd is created in the directory /etc or its subdirectory, a loop will be formed, as shown in the following figure:

At this time, if you execute rm -r /etc to delete the directory /etc, the directory /etc is not actually deleted because the inode and block of /etc still remain, making it an isolated directory. At this time, /etc cannot be accessed from the root directory.

2. Soft link

Unlike a hard link, a soft link creates an independent file with its own inode, but this file directs data reading to the file it is connected to. Since only the file is used as the pointing operation, when the source file is deleted, the soft link file will prompt "cannot be opened", and the deleted source file cannot be found.

Let's create a soft link and view the properties of the soft link:

#Create a soft link $ ln -s /etc/lvlv.txt lvlv_sb.txt

#View file attributes $ ll -i 
7792474 -rw---x--x 2 b3335 b3335 22 Nov 9 21:05 lvlv.txt
[b3335@MIC root]$ ll -i 
7792478 lrwxrwxrwx 1 b3335 b3335 13 Nov 10 15:23 lvlv_sb.txt -> /etc/lvlv.txt

It can be seen that the inode node of the soft link is different from the source file lvlv.txt, and the size of the soft link is exactly equal to the length of the string "/etc/lvlv.txt" 13. It can be seen that the soft link is a separate new file, and the content of the file is the path of the pointed file.

The following is a schematic diagram of a soft link, also taking the file lvlv.txt as an example.

The above is the detailed content of distinguishing Linux hard links and soft links. For more information about Linux hard links and soft links, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • linux No space left on device 500 error caused by inode fullness
  • How to view the behavior of search engine spider crawlers in Linux/Nginx
  • Linux gzip command compression file implementation principle and code examples
  • Use of Linux ipcs command
  • Detailed tutorial on deploying SpringBoot + Vue project to Linux server
  • 5 Commands to Use the Calculator in Linux Command Line
  • Use of Linux ls command
  • One line of code teaches you how to hide Linux processes
  • Use of Linux sed command
  • Detailed explanation of Linux index node inode

<<:  Eight ways to implement communication in Vue

>>:  Three ways to avoid duplicate insertion of data in MySql

Recommend

Summary of CSS sibling element floating analysis

float:left/right/none; 1. Same level floating (1)...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...

jQuery implements the mouse drag image function

This example uses jQuery to implement a mouse dra...

MySQL GROUP_CONCAT limitation solution

effect: The GROUP_CONCAT function can concatenate...

JS cross-domain solution react configuration reverse proxy

Cross-domain solutions jsonp (simulate get) CORS ...

How to set mysql permissions using phpmyadmin

Table of contents Step 1: Log in as root user. St...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which mea...

XHTML tags have a closing tag

<br />Original link: http://www.dudo.org/art...

How to view the network routing table in Ubuntu

What are Routing and Routing Table in Linux? The ...

Solve the cross-domain problem of Vue+SpringBoot+Shiro

Table of contents 1. Configure Vue front end 1. D...

How to install ionCube extension using pagoda

1. First install the pagoda Installation requirem...

A brief analysis of MySQL explicit type conversion

CAST function In the previous article, we mention...

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

Web Design Tips: Simple Rules for Page Layout

Repetition: Repeat certain page design styles thr...

Install and configure ssh in CentOS7

1. Install openssh-server yum install -y openssl ...