Difference between "soft link" and "hard link"

Guided reading We know that files have file names and data, which are divided into two parts on  Linux  : user data and metadata. User data, the file data block (data block), the data block is where the real content of the file is recorded; and metadata is the additional attributes of the file, such as file size, creation time, owner and other information. In Linux, the inode number in the metadata (inode is part of the file metadata but does not contain the file name, the inode number is the inode number) is the unique identification of the file rather than the file name. The file name is only for the convenience of people's memory and use, and the system or program searches for the correct file data block through the inode number. And there is a way to quickly find data elements, that is, soft and hard links, let's take a look.
What is a link?

Links are simply a way of file sharing, a concept in POSIX, and all major file systems support linked files.

What is it used for?

You can simply understand the link as a common shortcut in Windows (or a stand-in in OS X), it is commonly used in Linux to solve some library version problems, and usually links some files with deeper directory levels to a in a more accessible directory. For these purposes, we usually use soft links (also called symbolic links).

content

What is the difference between soft links and hard links?
Now let's get to the point and discuss what is the difference between soft and hard links?

First of all, from the point of view of use, there is no difference between the two, both are the same as the normal file access method, support reading and writing, and can be directly executed if it is an executable file.

Where is the difference? on the underlying principle.

To explain clearly, we first create a file in one of our own working directories, and then create a link to this file:

$ touch myfile && echo "This is a plain text file." > myfile
$ cat myfile

This is a plain text file.

Now we've created a file that can't be ordinary anymore. Then we create a hard link to it and take a look at the current directory:

$ ln myfile hard
$ ls -li

25869085 -rw-r--r-- 2 unixzii staff 27 7 8 17:39 hard
25869085 -rw-r--r-- 2 unixzii staff 27 7 8 17:39 myfile

In the leftmost column of the ls result is the inode value of the file, which you can simply think of as a pointer in the C language. It points to a block of the physical hard disk. In fact, the file system will maintain a reference count. As long as there is a file pointing to this block, it will not disappear from the hard disk.

You can also see that these two files are like one file, with the same inode value, and both point to the same block.

Then we modify the hard link file we just created:

$ echo "New line" >> hard
$ cat myfile

This is a plain text file.
New line

As you can see, these two files are really one file. Let's take a look at the difference between a soft link (that is, a symbolic link) and it.

$ ln -s myfile soft
$ ls -li

25869085 -rw-r--r-- 2 unixzii staff 36 7 8 17:45 hard
25869085 -rw-r--r-- 2 unixzii staff 36 7 8 17:45 myfile
25869216 lrwxr-xr-x 1 unixzii staff 6 7 8 17:47 soft -> myfile

Hey, you will find that the inode of this soft link is not the same, and its file attribute also has an l flag, which means that it is not the same type as the two files we created before.

Let's try to delete the myfile file, and then output the file contents of the soft and hard links respectively:

$ rm myfile
$ cat hard

This is a plain text file.
New line
$ cat soft

cat: soft: No such file or directory

The previous hard link has no effect at all, because the block pointed to by its inode has a hard link pointing to it, so the block is still valid and accessible. However, the content pointed to by the inode of the soft link actually saves an absolute path. When the user accesses the file, the system will automatically replace it with the file path it refers to. However, the file has been deleted, so naturally The file cannot be found.

To test this conjecture, let's write something to this soft link:

$ echo "Something" >> soft
$ ls

hard myfile soft

As you can see, the myfile file that was just deleted appears again! This means that when we write to access a soft link, the system automatically replaces its path with the absolute path it represents, and directly accesses that path.

Summarize

To sum up, we can actually summarize
here:

Hard link: no different from ordinary files, inodes all point to the same file in the block on the hard disk
Soft link: saves the absolute path of the file it represents, is another kind of file, has an independent block on the hard disk, access replaces its own path.

The original text comes from: https://www.linuxprobe.com/soft-and-hard-links.html


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859272&siteId=291194637