Soft links and hard links in linux

 

There are two types of Linux links, one is called Hard Link and the other is called Symbolic Link.
By default, the ln command produces hard links.

 

Hard link: Hard link refers to the connection through the inode. In the Linux file system, no matter what type of file is stored in a disk partition, it is assigned a number called an inode number (Inode Index). In Linux, it is possible for multiple filenames to point to the same inode. Usually this connection is a hard connection. The role of the hard link is to allow a file to have multiple valid path names, so that the user can establish a hard link to an important file to prevent the function of "misdeletion". The reason for this is as described above, because there is more than one connection to the inode of the directory. Deleting only one connection does not affect the inode itself and other connections. Only when the last connection is deleted, the data block of the file and the connection of the directory will be released. That is, the condition for a file to be truly deleted is that all hard-linked files associated with it are deleted.

 

Soft connection: Another connection is called a symbolic link (Symbolic Link), also called a soft connection. Soft link files have Windows-like shortcuts. It is actually a special file. In a symbolic link, a file is actually a text file that contains information about the location of another file.

 

For example:
[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 a file of f1 Symbolic link file f3
[oracle@Linux]$ ls -li # The -i parameter displays the inode node information of the file
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 node of the hard-connected file f2 and the original file f1 The same, both are 9797648, but 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
It can be seen from the above test that when the original file f1 is deleted, the hard link f2 is not affected, but the symbolic link f1 file is invalid

 

Guess you like

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