The difference between hard and soft links in Linux

First, the concept of
hard connection refers to the connection through the index node. In the Linux file system, no matter what type of file is stored in the disk partition, it is assigned a number, called the Inode Index. In Linux, multiple file names refer to the same index node. For example: A is a hard link of B (A and B are both file names), then the inode node number in the directory entry of A is the same as the inode node number in the directory entry of B, that is, one inode node corresponds to two different files Name, two file names point to the same file, A and B are completely equal to the file system. Deleting any of them will not affect the other's access.
The other kind of connection is called symbolic link (Symbolic Link), also called soft link. Soft link files have shortcuts similar to Windows. It is actually a special file. In the symbolic link, the file is actually a text file, which contains the location information of another file. For example: A is a soft link of B (A and B are both file names), the inode node number in the directory entry of A is different from the inode node number in the directory entry of B, A and B point to two different Inode, then points to two different data blocks. However, only the path name of B is stored in the data block of A (the directory entry of B can be found according to this). There is a "master-slave" relationship between A and B. If B is deleted, A still exists (because the two are different files), but it points to an invalid link.
Second, the experimental results
[oracle @ Linux] $ touch f1 #create a test file f1
[oracle @ Linux] $ ln f1 f2 #create a hard link file f2 of f1
[oracle @ Linux] $ ln -s f1 f3 #create f1 A symbolic link file f3
[oracle @ Linux] $ ls -li # -i parameter displays the file's 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

As can be seen from the above results, the inode nodes of the hard link file f2 and the original file f1 are the same, both are 9979648, but the inode nodes of the symbol link file are 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

can be seen by the above test: when After deleting the original file f1, the hard link f2 is not affected, but the symbolic link f1 file is invalid
Results (1). Delete the symbolic link f3, which has no effect on f1, f2; 2). Delete the hard link f2, which has no effect on f1, f3; 3). Delete the original file f1, which has no effect on the hard link f2, resulting in the symbol Connection f3 is invalid; 4). Delete the original file f1 and hard link f2 at the same time, the entire file will be deleted.

3. Different syntax format:

Hard link: ln Source file Link name Soft link:

    ln -S source file link name

Fourth, the establishment of a soft link is to create a new file. When accessing the linked file, the system will find that it is a linked file, and it reads the linked file to find the file to be accessed. The hard link is a pointer to the file index node. The system does not redistribute the inode for it. Only the super user can create a hard link for the directory.

 

Guess you like

Origin www.cnblogs.com/1314-520/p/12673998.html