Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the hard links and soft links of Linux

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to understand hard links and soft links of Linux", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to understand hard links and soft links of Linux" article.

Hard link

A hard link is just an entry in the partition table that points to an area on disk indicating that that area has been assigned to a file. In other words, a hard link points to data that has been indexed by another entry. Let's see how it works.

Open the terminal, create an experimental directory and enter:

Mkdir test_dircd test_dir

Create a file using touch:

Touch test.txt

In order to get more experience. Open test.txt in a text editor and add some words

Now establish a hard link by executing the following command:

Ln test.txt hardlink_test.txt

Run ls and you will see that your directory now contains two files, or so it looks. As you read earlier, what you really see are two names of exactly the same file: hardlink_test.txt contains the same content, does not fill any more space on the disk (you can try to test it with a large file), and uses the same inode as test.txt:

$ls-li * test*16515846-rw-r--r-- 2 paul paul 14 oct 12 09:50 hardlink_test.txt16515846-rw-r--r-- 2 paul paul 14 oct 12 09:50 test.txt

The-I option of ls displays the "inode value" of a file. "inode" is a block of information in a partition table that contains the location of a file or directory on disk, when it was last modified, and other data. If two files use the same inode, they are actually the same file regardless of their location in the directory tree.

Soft link

Soft links, also known as symbolic links symlink, are different from hard links: soft links are actually a separate file with its own inode and its own small chunks on disk. But it contains only a small piece of data that points the operating system to another file or directory.

You can use the-s option of ln to create a soft link:

Ln-s test.txt softlink_test.txt

This creates a soft link softlink_test.txt in the current directory, which points to test.txt.

Execute ls-li again, and you can see the difference between the two links:

$ls-litotal 816515846-rw-r--r-- 2 paul paul 14 oct 12 09:50 hardlink_test.txt16515855 lrwxrwxrwx 1 paul paul 8 oct 12 09:50 softlink_test.txt-> test.txt16515846-rw-r--r-- 2 paul paul 14 oct 12 09:50 test.txt

Hardlink_test.txt and test.txt contain some text and literally occupy the same space. They use the same inode value. At the same time, softlink_test.txt takes up much less and has different inode values, marking it as a completely different file. Using the-l option of ls also displays the file or directory that the soft link points to.

Why use links?

They are suitable for applications with their own environment. Your Linux distribution usually doesn't come with the * * version of the application you need. Take the excellent Blender 3D design software as an example. Blender allows you to create 3D still images and animated movies that everyone wants to have on their own machine. The problem is that the current version of Blender is at least one version higher than the one that comes with any distribution.

Fortunately, Blender provides downloads that can be used out of the box. In addition to the program itself, these packages contain complex libraries and dependency frameworks that Blender needs to run. All of these data and blocks are in their own directory hierarchy.

Every time you want to run Blender, you can cd to the folder where you downloaded it and run:

. / blender

But it's inconvenient. It would be better if you could run blender commands from anywhere in the file system, such as the desktop command launcher.

The way to do this is to link the blender executable to the bin/ directory. On many systems, you can make the blender command available by linking it to anywhere in the file system, like this.

Ln-s / path/to/blender_directory/blender / home//bin

Another situation where you need links is that the software needs outdated libraries. If you use ls-l to list your / usr/lib directory, you will see a lot of soft link files flash by. If you take a closer look, you will see that soft links usually have similar names to the original files they link to. You may see a link from libblah to libblah.so.2, and you may even notice that libblah.so.2 links to the original file libblah.so.2.1.0.

This is because applications usually need to install libraries that are older than the installed version. The problem is that even if the new version is still compatible with the old version (usually), the program will have a problem if it cannot find the version it is looking for. To solve this problem, distributions usually create links so that picky applications believe it has found the old version, when in fact it only finds a link and ends up using the newer version of the library.

Some are related to programs that you compile from source code. The program you compile yourself usually ends up under / usr/local, and the program itself ends up in / usr/local/bin, which looks for the libraries it needs in the / usr/local/bin directory. But suppose your new program needs libblah, but libblah is in / usr/lib, and that's where all other programs will find it. You can link it to / usr/local/lib by doing the following:

Ln-s / usr/lib/libblah / usr/local/lib

Or if you like, you can cd to / usr/local/lib:

Cd / usr/local/lib

Then use the link:

Ln-s.. / lib/libblah above is about "how to understand Linux hard links and soft links" this article content, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report