Detail linux soft and hard links

  • 2020-05-12 06:46:26
  • OfStack

This article describes linux soft and hard links in detail, but without further elaboration, read on.

1 link file

There are two types of Linux links. One is called a hard link (Hard Link), and the other is called a symbolic link (Symbolic Link). By default, the ln command generates hard links.

[soft connection]

The other type of join is called a symbolic join (Symbolic Link) or a soft join. Soft link files have shortcuts similar to Windows. It's actually a special file. In a symbolic connection, the file is actually a text file containing the location of another file.

Link files can even link to files that do not exist, creating what is known as a "broken link" problem (or "phenomenon "). Link files can even loop through themselves. Similar to recursion in programming languages.

Use the ln-s command to generate a soft connection, as follows:


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

When the symbol file is read or written, the system will automatically convert the operation to the source file, but when the link file is deleted, the system will only delete the link file, not the source file itself.

ps: add soft links to directories

1. Either the source file address or the destination file address must be an absolute path, otherwise there will be an error like "too many symbolic connection layers"

Hard connection
Hard join refers to connecting through an index node. In Linux's file system, the file stored on the disk partition is assigned a number, no matter what type, called the index node number (Inode Index). In Linux, multiple file names pointing to the same 1 index node exist. This type of connection is a hard connection. Hardwiring is the ability to allow a file to have multiple valid path names so that users can hardwire to important files to prevent "accidental deletion." The reason for this is described above, because there are more than one connection to the index node that should be in the directory. The removal of only one connection does not affect the index node itself and other connections. Only when the last connection is removed will the data block of the file and the directory connection be released. That is, the file is actually deleted only if all the hardwired files associated with it are deleted.

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


ln -d existfile newfile 

Hard linked files have two limitations

1) hard links to directories are not allowed;

2) links can only be created between files in the same file system as 1.

When you read, write, and delete hard linked files, the results are the same as for soft links. But if we delete the hard link file's source file, the hard link file still exists and retains the content we want.

At this point, the system "forgets" that it was once a hard-linked file. Instead, think of it as an ordinary document.

The difference between the two

A hard connection is a connection made through an index node. On Linux's file system, the file stored on the disk partition is assigned a number, no matter what type, called the index node number (Inode Index).

In Linux, multiple file names pointing to the same 1 index node exist. This type of connection is a hard connection. The purpose of a hard connection is to allow one file to have multiple valid path names so that the user can establish a hard connection to the important

File to prevent "error delete" function. The reason for this is described above, because there are more than one connection to the index node that should be in the directory. The removal of only one connection does not affect the index node itself and other connections, only when the last one is removed

After the connection is deleted, the data block of the file and the directory connection are released. That is, the file is actually deleted.

Soft link files are somewhat similar to Windows shortcuts. It's actually a special file. In a symbolic connection, the file is actually a text file containing the location of another file.

Experiment to deepen understanding


[oracle@Linux]$ touch f1     # create 1 Test files f1
[oracle@Linux]$ ln f1 f2     # create f1 the 1 Hard connection files f2
[oracle@Linux]$ ln -s f1 f3    # create f1 the 1 Symbol connection file f3
[oracle@Linux]$ ls -li      # -i Parameter display 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

It can be seen from the above results that the hard connection file f2 is the same as the inode node of the original file f1, both of which are 9797648, while the inode node of the symbolic connection 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

As can be seen from the above test, when the original file f1 is deleted, the hard connection f2 is not affected, but the symbolic connection f1 file is invalid

3. Summary

Based on this, you can do some relevant tests and get the following conclusions:
1). Delete the symbol connection f3, which has no effect on f1 and f2;
2). Delete the hard connection f2, which has no effect on f1 and f3;
3). Delete the original file f1, which has no effect on the hard connection f2, resulting in the failure of the symbolic connection f3;
4). Delete the original file f1 at the same time, hardconnect f2, and the whole file will be deleted.


Related articles: