[Linux] hard link and soft link

In order to facilitate users to access files, Linux provides a mechanism called link, which can associate a file or directory with another file or directory, so as to achieve the effect of multiple paths pointing to the same file or directory.

I. Overview

Linux is a multi-user and multi-tasking operating system that provides a flexible file system to store and manage files on different partitions, devices and networks.

In order to facilitate users to access files, Linux provides a mechanism called link, which can associate a file or directory with another file or directory, so as to achieve the effect of multiple paths pointing to the same file or directory .

Links in Linux are divided into two types: hard links (hard link) and soft links (symbolic link). This article will detail the characteristics, usage, and differences of these two types of connections.

  • A hard link refers to associating a file name with an existing file in the same file system, so that the file name can also access the file. The hard link shares the inode with the original file, that is, they have the same inode number and the same device number. Therefore, for the hard link and the original file, their access rights, owner, size and other attributes are the same.

  • A soft link (also called a symbolic link) refers to associating a file name with another file between different file systems, so that the file name can also access the file. Soft links and original files do not share inodes, they have different inode numbers and device numbers. Therefore, for soft links and original files, their attributes such as access rights, owners, and sizes may be different .


At this point, you have roughly understood the function of the link.

2. Detailed explanation of hard links and soft links

Hard links and soft links are two different file system objects, and they can both be used to create associations between files. However, their working principles and characteristics are quite different. This section will introduce the definitions, creation methods, advantages and disadvantages, usage scenarios and deletion methods of hard links and soft links in detail.

2.1 Hard link

2.11 Definition of Hard Links

A hard link refers to the situation where multiple file names in a file system point to the same data block (inode). That is, hard links are different aliases of the same file that share the same content, attributes, and permissions. Hard links can only be created within the same partition and cannot span different file systems.

Additional knowledge point 1: inode

inodeIt is a concept in the file system, also known as the file index node, which contains the basic information of the file and the pointer of the data block.

Meta information is similar to file attributes, including file creator, creation date, file size, file permissions, etc. The actual information is stored in blocks, and the area where file meta information is stored is called an inode. A file must occupy one inode and at least one block.

Additional Knowledge Point 2: File System

In a Linux system, many different file system types can be supported, and so is Windows. Related commands have been written in the previous article .

The following figure is an example: the root directory (/) is an ext4 type file system with a size of 40G, 21G is used, and the remaining 18G is 54% used. It is mounted on /dev/vda2. Other subdirectories under the directory, such as /home, /usr, /var, etc., are all part of the ext4 file system, unless I assign different file systems to them separately (for example, /dev in the figure below is the udev file system).
insert image description here

Pay attention to the distinction between file system and file system type, which are two concepts.

  • Hard links can usually only be created within the same file system. Even if two filesystems are of type ext4, hard links cannot be created directly between them.
  • Usually: a partition can only have one file system, for example, my server has only one 40GB SSD system disk, which is divided into 2 partitions:
    insert image description here
    I can’t continue to partition this disk, it will damage the system. Therefore, I bought another 10GB cloud hard disk, divided it into two partitions, and established a file system on the two partitions, the types are ext4 and ext3 respectively.

    As shown in the picture, I have mounted it here:
    insert image description here

2.12 How to create hard links

In Linux systems, you can use lncommands to create hard links. For example, if you want to file1create a hard link to a file file2, you can execute the following command:

ln file1 file2

file2In this way, a hard link named db will be generated in the current directory , which file1points to the same data block. You can use ls -lithe command to view the inode number and hard link number of the file. For example:

ls -li file1 file2

Example: There are the following files:/root/LinuxC/Link/folder/origin.txt

Create a hard link in its parent folder:ln folder/origin.txt new.txt

Now a new.txt file will be generated with the same content as origin.txt:

insert image description here

It can be seen that the inode numbers of the two files are both 5962, indicating that they point to the same data block. At the same time, the number of hard links of the two files is 2, indicating that there are two file names pointing to the data block. The creation time of the two files is the same, actually the creation time of the original file.

Because they point to the same data block, no matter which file is modified, the remaining files with hard links to it will also change:
insert image description here


As emphasized earlier, only files in the same file system can create hard links. I have /root/data/ext4mounted a file system before: /dev/vdb1, and the type is also ext4.

I now go to this directory and try to create hard links between 2 filesystems for 2 files:

insert image description here
cannot be created.

Why can't hard links be created between two file systems? This is because:

  • Hard links must point to the same inode, and different file systems have different inode structures and numbers. If hard links point to different filesystems, there is no guarantee of inode consistency and validity.
  • Hard links must point to the same volume, and different file systems may be on different disks or partitions. If the hard links point to different volumes, data integrity and accessibility cannot be guaranteed.

2.13 Advantages and disadvantages of hard links

Hard links have the following advantages:

  • Hard links save storage space because they don't take up additional data blocks.
  • Hard links protect files from accidental deletion because a block of data is only freed when all filenames pointing to it are deleted.
  • Hard links can improve file access efficiency because they do not require an additional lookup process.

Hard links also have the following disadvantages:

  • Hard links cannot span different file systems, because different file systems may have different inode allocation methods.
  • Hard links cannot be used for directories, as this would lead to confusing directory structures and circular references.
  • Hard links cannot reflect changes in the original file, such as modification time and permissions.

2.14 Usage Scenarios of Hard Links

Hard links are suitable for some of the following scenarios:

  • Hard links can be used when multiple aliases need to be created for the same file.
  • Hard links can be used when important files need to be protected from accidental deletion.
  • When you need to improve the efficiency of frequently accessing files, you can use hard links.

2.15 How to delete hard links

In Linux systems, you can use rmcommands to delete hard links. For example, if you want to delete file2this hard link, you can execute the following command:

rm file2

In this way, file2the file name will be deleted, but file1the data block will not be affected. The data block will only be freed when all filenames pointing to the data block are deleted . For example:

rm file1

file1In this way, the filename is deleted and the data block is freed.

2.2 Soft link

2.21 Definition of soft link

A soft link (also known as a symbolic link or symlink) is a special type of file that contains path information for another file or directory. That is, a soft link is a shortcut that points to another object that does not share the same content, attributes, and permissions. Soft links can be created across different partitions and filesystems .

2.22 How to create a soft link

In Linux systems, you can use ln -scommands to create soft links. For example, if you want to file1create a soft link to a file file3, you can execute the following command:

ln -s file1 file3

In this way, a soft link named _ will be generated in the current directory file3, which points to file1the path where it is located. You can use ls -lthe command to view the file type and link target. For example:

ls -l file1 file3

Example:
insert image description here

It can be seen that soft links can be established between different file systems, new.txtand the file type is l, indicating that it is a soft link. At the same time, new.txtthe target of the link is origin.txt, which means that it points to origin.txtthe path where it is located.

The soft link is just a "shortcut" of the source file, so opening and modifying a soft link file is actually opening and modifying the source file (but deleting is just deleting the "shortcut").

insert image description here

If the source file is deleted, the soft link file will become a dead link and cannot be accessed. If the dead link file is modified, a source file with the same name will be recreated and a new link relationship will be established. This situation should be avoided, because it may overwrite the original source file or cause confusion.

2.23 Advantages and disadvantages of soft links

Soft links have the following advantages:

  • Soft links can span different filesystems because they only store path information , not inode numbers.
  • Soft links can be used for directories because they don't lead to clutter and circular references in the directory structure.
  • Soft links can reflect changes in the original file, such as modification time and permissions.

Soft links also have the following disadvantages:

  • Soft links take up additional storage space because they require a separate data block to store path information .
  • Soft links may fail because they cannot find the correct path when the original file is moved or deleted.
  • Soft links can reduce file access efficiency because they require an additional lookup process.

2.24 Usage scenarios of soft links

Soft links are suitable for some of the following scenarios:

  • Soft links can be used when you need to create shortcuts to files or directories in different locations or on different file systems.
  • Soft links can be used when you need to create dynamic file or directory associations.
  • Soft links can be used when the attributes and changes of the original file need to be preserved.

2.25 How to delete soft links

In the Linux system, you can use rmthe command to delete the soft link. For example, if you want to delete file3this soft link, you can execute the following command:

rm file3

file3In this way, the file name will be deleted , but it will not affect file1or the path where it is located. Only when the original file is moved or deleted, the soft link will become invalid. For example:

mv file1 file4

In this way, it will move file1to another location and make it file3impossible to find the correct path.


Popular metaphors: rough metaphors, don't go into details.

  • A hard link is equivalent to attaching different tags to the same book. Each tag can be used to find the book, but there is only one book. If you remove one of the labels, the book is still there, just one less label. But if you throw the book away, all tags are lost. And you can't apply the same label to different books, or to a book that's not in your hand.
  • A soft link is equivalent to writing a note to a book, which says where the book is, and you can use this note to find the book. But this note is not part of the book, it's just a guide. If you tear up the note, the book is still there, you just won't be able to find it. But if you have moved or lost the book, this note is of no use. And you can write a note to any book, no matter where the book is, and you can write multiple notes to the same book.

insert image description here

3. Find the link file of a file

A file has several links, how to find these linked files?

(1) Obtain inodethe number of the file

insert image description here
new.txt has 3 hard links, and the inode number is 5962.

(2) Find

find / -inum 5962

It stands to reason that the result should only show 3 files. But the result does have 10:
insert image description here

This could be due to one of several reasons:

  • Soft Links: Soft links are another type of link, which is different from hard links. A soft link is a symbolic link pointing to the original file or directory, which has its own inode. So if you create a soft link on a hard linked file, then the soft link will have a different inode number.

  • System files: Some operating systems or file systems automatically create some system files, or metadata files, which are associated with your file and thus have a different inode number.

  • Other hard links: If you have created hard links in other directories or folders, and those hard links point to your files, then those hard links will also have different inode numbers.

Four, hard links, soft links and pointers, references

Through the introduction of this article, you may feel that hard links and soft links are somewhat similar to pointers and references in C/C++.

Let's review pointers and references first:

  • A pointer is a variable that stores the memory address of another variable. Through a pointer, you can indirectly access or modify the value of the variable it points to. Pointers can do arithmetic, can point to different variables, and can be left uninitialized.
  • Reference is a new way of using variables introduced in C++, which is equivalent to giving an alias to an existing variable . By reference, you can directly access or modify the value of the variable it refers to. A reference must be initialized, and the variable it refers to cannot be changed.

    The C language itself does not support references, only C++ does. References in C++ are implemented through pointers at the bottom, but it is easier to use and safer than pointers.

Soft links and hard links under Linux have some similarities with C language references and pointers:

  • A soft link is similar to a pointer in C language. It is a special file that contains the path information of another file or directory. Through a soft link, you can indirectly access or modify the contents of the file or directory it points to. Soft links can span different file systems, or point to files that don't exist.
  • A hard link is similar to a reference in C language, it is a file name that shares the same inode as the source file. Through a hard link, the contents of the file it refers to can be directly accessed or modified. Hard links can only be created within the same file system, not directories, nor can they point to non-existent files.

However, there are many differences between soft links and hard links and references and pointers in C language:

  • The difference between a soft link and a pointer is that a soft link itself is an entity, which occupies a certain amount of disk space. When the source file is deleted, the soft link will become a dead link and cannot be accessed.
  • The difference between a hard link and a reference is that a hard link is not an alias of the source file, but an independent file name. When the source file is deleted, the hard link is still valid.


Write love you forever into the end of the poem ~

Guess you like

Origin blog.csdn.net/weixin_43764974/article/details/131492755