Home  >  Article  >  System Tutorial  >  Thoroughly understand Linux hard links and soft links

Thoroughly understand Linux hard links and soft links

PHPz
PHPzforward
2024-02-13 20:20:02823browse

Hard links and soft links are two common link file types under Linux systems. If you want to learn more about the concept of hard links and soft links, I will take you through the research. By reading this article, you will fully understand the differences and characteristics of hard links and soft links.

First of all, under the Linux system, there are two types of link files, one is a hard link (Hard Link) and the other is a soft link, also called a symbolic link (Symbolic Link).

Hard link

In Linux systems, you can use the ln command to create a hard link:

ln [parameter] [source file or directory] [destination file or directory] Thoroughly understand Linux hard links and soft links

In the file system, each file will have an index node (Inode Index). This index node is used to identify the file. Hard links are linked through this index node. Hard links can only be created between files in the same file system, and directories cannot be created. However, multiple hard links can be created, that is, multiple files can point to the same index node, or one file can have Multiple path names, so one file can correspond to multiple file names.

We can use the ls command to view the source file and the index node of the hard link we just created:

Thoroughly understand Linux hard links and soft links

From the picture, I can see that the index nodes of the source file and the hard link file are the same, so the source file and the hard link file point to the same index node.

So, why can't hard links be created in directories? Let's give it a try.

Thoroughly understand Linux hard links and soft links

As shown in the picture above, users are not allowed to create hard links for directories. Everyone knows the directory tree. If you create a hard link for a directory, let's think about it carefully. In this case, will the directory tree change? It is circular. When we want to locate the files in this directory, we cannot locate them by name.

A hard link is equivalent to creating a copy of the source file. If the content of the hard link file is modified, the content of the source file will also change.

When we were developing under the Linux system, have we ever encountered the situation of accidentally deleting some files, and then trying to retrieve them but being unable to do anything about them? What should we do?

If you don’t know how to restore, then you will learn from the experience and gain wisdom. Next time I will create a few more hard links, so that this file will have multiple links. Even if one of the links is deleted, it will be fine. Additional source files or other hard link files can still be used, but deleting one of the links will not affect the index node and other links, because only the link from the file to the index node is deleted, as long as the last and only link is not deleted. That's fine, I still have to save the last straw.

Let’s try to delete the hard link and see:

Thoroughly understand Linux hard links and soft links

However, in a Linux system, there will be multiple file systems, and these file systems will be located on different disks or partitions. As mentioned earlier, hard links can only be created under the same file system. Isn’t it a bit inconvenient?

Yes, hard links can only be like this, but how can we smart humans only be like this? At this time, soft links come into play, so what happened to soft links?

Soft link

Under Linux systems, soft links can share files or directories, just like shortcuts under Windows.

We can simply regard the content of a soft link as a link or a path. When we use a soft link, we access the content of the link or path through the content of the soft link, just like we use www.baidu. com link to access Baidu, or use sudo vim /etc/network/interfaces under Ubuntu to access the content in the path /etc/network/interfaces.

As mentioned earlier, hard links can only be created under the same file system, so soft links will not have this restriction. It can be understood that soft links are created to get rid of this restriction.

Soft link files can point to any file or directory in any file system. Not only that, a soft link can also point to a non-existent file, but you cannot create a hard link to a non-existent file!

At this point, you should know what a soft link is!

Use the ln command to create a soft link under the Linux system, which is similar to the command to create a hard link. Let’s give it a try:

\1. Create soft link:

Thoroughly understand Linux hard links and soft links

\2. Delete soft link:

Thoroughly understand Linux hard links and soft links

We can see from the picture above that when the source file is deleted, the name of the soft link file is still displayed in the directory, but when we actually view the soft link file, it actually no longer exists and becomes The dead link is still displayed in the directory. In fact, this is not difficult to understand. Let’s think about the shortcuts in Windows. It is also like this. When we delete all the source files of an APP installed in Windows , its shortcut is still displayed on the desktop, but when we click the shortcut with the mouse, a window will pop up indicating that the file no longer exists.

Thoroughly understand Linux hard links and soft links

When we don’t want the shortcut on the Windows desktop, we can just delete the shortcut directly. The same is true for the soft link. We can just delete the soft link directly.

When we create soft links, we should pay attention to a small problem. Don't use relative paths like me. That is, you must use absolute paths, not relative paths. Soft links created by relative paths cannot be accessed, and an error will be reported when accessing:

Too many levels of symbolic links.

When you see this line reporting an error, changing to an absolute path will solve the problem.

Below I will briefly talk about two places where I use soft links in actual scenarios:

\1. Sometimes we always need to execute a lot of commands, and the executed commands correspond to the paths of different directories. Some paths are really long and difficult to remember. It is really laborious to type out the entire path, and the efficiency is also reduced. If it is low, then the role of soft links will be reflected. If you create a soft link to these long and smelly paths under the current path, will the path be much simpler?

\2. I have installed many kinds of cross-compilers in the virtual machine, and different compilers may use different third-party libraries. The method must be to put the header files in the path of the compiler. The header file will be automatically found when compiling the program, but this library file will be relatively large. If each compiler needs to use it, moving each one will take up more space. At this time, the role of soft link comes, through To create a soft link, we create a soft link and share the library file, which can also reduce disk space usage.

There are many functions of soft links. We need to flexibly use the functions of soft links in different scenarios. In the end, it may make our work easier or improve work efficiency.

In actual applications, hard links are used less and soft links are used more.

The above is the detailed content of Thoroughly understand Linux hard links and soft links. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete