Table of Contents
Hard link" >Hard link
Soft link" >Soft link
Home System Tutorial LINUX Thoroughly understand Linux hard links and soft links

Thoroughly understand Linux hard links and soft links

Feb 13, 2024 pm 08:20 PM
linux linux tutorial linux system linux command shell script overflow Getting started with linux linux learning

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).

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?

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

Video Face Swap

Video Face Swap

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

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 computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles