English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Linux Soft Link and Hard Link

This article introduces Linux soft link and hard link in detail. There is no more chatter, let's continue to read.

I. Link file

Linux links are divided into two types, one called hard link (Hard Link), and the other called symbolic link (Symbolic Link). By default, the ln command generates a hard link.

【Symbolic Link】

Another type of connection is called symbolic link (Symbolic Link), also known as soft link. The soft link file is similar to the shortcut in Windows. It is actually a special file. In symbolic links, the file is actually a text file that contains the location information of another file.

The link file can even link to a non-existent file, which produces the problem (or phenomenon) generally referred to as 'broken link', and the link file can even link to itself in a circular manner. It is similar to recursion in programming languages.

Use ln -The 's' command can generate a symbolic link, as follows:

[root@linux236 test]# ln -s source_file softlink_file

When performing read or write operations on symbolic files, the system will automatically convert the operation to the operation on the source file, but when deleting the link file, the system only deletes the link file and does not delete the source file itself.

ps: add a soft link to the directory

 1Both the source file address and the target file address must use an absolute path, otherwise an error such as 'Too many levels of symbolic link' will occur

【Hard Link】
Hard link refers to the connection through an index node. In the Linux file system, all files stored in a disk partition are assigned a number, called the Inode Index (Inode Index), regardless of their type. In Linux, multiple filenames can point to the same index node, which is generally called a hard link. The function of hard link is to allow a file to have multiple valid path names, so that users can establish hard links to important files to prevent the function of 'accidental deletion'. The reason is as described above, because there is more than one connection to the index node of the corresponding directory. Deleting only one connection does not affect the index node itself and other connections. Only when the last connection is deleted, the data blocks of the file and the connection of the directory will be released. That is to say, the real condition for deleting a file is that all the hard link files related to it are deleted.

info ln command tells you that a hard link is another name for an existing file (A "hard link" is another name for an existing file), which is somewhat confusing. The command for hard links is}}

ln -d existfile newfile 

There are two limitations of hard link files

1) Hard links to directories are not allowed;

2) Only links between files in the same file system can be created.

Reading and writing, and deleting operations on hard link files are the same as those on soft links. But if we delete the source file of a hard link file, the hard link file still exists and retains all the original content.

At this point, the system 'forgets' that it was a hard link file. And treats it as a regular file.

The difference between them

Hard links refer to connections made through index nodes. In the Linux file system, all files stored in the disk partition are assigned a number called the index node number (Inode Index) regardless of their type.

In Linux, it is possible for multiple filenames to point to the same index node. Generally, such links are hard links. The function of hard links is to allow a file to have multiple valid path names, so that users can create hard links to important

feature to prevent 'accidental deletion'. The reason is as described above, because there is more than one link to the index node corresponding to the directory. Deleting only one link does not affect the index node itself and other links; only when the last

The file data blocks and directory links are only released after the link is deleted. That is to say, the file is truly deleted.

Soft links are somewhat similar to Windows shortcuts. They are actually a type of special file. In symbolic links, the file is actually a text file that contains the location information of another file.

Enhance understanding through experiments

[oracle@Linux]$ touch f1     # Create a test file f1
[oracle@Linux]$ ln f1 f2     # Create f1a hard link file f2
[oracle@Linux]$ ln -s f1 f3    # Create f1a symbolic link file f3
[oracle@Linux]$ ls -li      # -i parameter shows the file inode node information
total 0
9797648 -rw-r--r-- 2 oracle oinstall 0 Apr 21 08:11 f1
9797648 -rw-r--r-- 2 oracle oinstall 0 Apr 21 08:11 f2
9797649 lrwxrwxrwx 1 oracle oinstall 2 Apr 21 08:11 f3 -> f1

From the above results, we can see that the hard link file f2with the original file f1the inode node is the same, both are9797648, however, the inode node of the symbolic link file is different.

[oracle@Linux]$ echo "I am f1 file" >>f1
[oracle@Linux]$ cat f1
I am f1 file
[oracle@Linux]$ cat f2
I am f1 file
[oracle@Linux]$ cat f3
I am f1 file
[oracle@Linux]$ rm -f f1
[oracle@Linux]$ cat f2
I am f1 file
[oracle@Linux]$ cat f3
cat: f3: No such file or directory

From the above test, we can see that when deleting the original file f1after2No effect, but the symbolic link f1File invalid

3. Summary

Thus, you can do some related tests and get the following conclusions:
1). Delete the symbolic link f3, for f1, f2no effect;
2). Delete the hard link f2, for f1, f3also has no effect;
3). Delete the original file f1). Delete the original file f2No effect, causing the symbolic link f3invalid;
4). Delete the original file f1, hard link f2, the entire file will be truly deleted.

That's all for this article. I hope it will be helpful to everyone's learning, and I also hope everyone will support the Yana tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)

You May Also Like