Linux hard links and soft links deep understanding

  
                  In the process of learning Linux, I often link soft and hard, so I checked some information today and then integrated it into my blog for review and review. On the one hand, it is also because of the vamei's concept and system series of linux, I feel that it is not complete. Soft links are equivalent to shortcuts in windows. A hard link is equivalent to a disaster recovery system. The data is stored in two places. Unlike replication, there is a synchronization mechanism between two places. One data change is synchronized to another in real time. In addition, if one data is deleted. It will not affect the data of another place. Detailed introduction: The hard link points to the node (inode), which is another name of the existing file, modify one of them, and the file connected with it is modified at the same time; When reading, writing, and deleting, the effect is the same as the soft link. But if we delete the source file of the hard link file, the hard link file still exists and the original content is retained. At this time, the system “forgot” that it used to be a hard link file and treated him as a normal file. Hard link files have two limitations: 1. It is not allowed to create hard links to directories; 2. Links can only be created between files in the same file system. The soft link points to the path, also called the symbolic link. This file contains the path name of another file, which can be any file or directory. It can also link files of different file systems, similar to the shortcuts under win. Soft link files can even link files that don't exist, which creates a problem that is commonly referred to as "broken chain" (or 现象 phenomenon"), and link files can even link themselves, similar to programming languages. Recursive. Example: ln exitfile newfileln -s exitfile newfile The newfile here is the name of the created link. The first one has no parameters. The default is a hard link. The second -s indicates a soft link. Ls -il can view attributes such as the inode number of the file, for example: [root@server tmp]# touch file[root@server tmp]# ln file file1[root@server tmp]# ln -s file file2[root@server tmp ]# ls -iltotal 027127 -rw-r--r--. 2 root root 0 Nov 20 10:40 file27127 -rw-r--r--. 2 root root 0 Nov 20 10:40 file127132 lrwxrwxrwx. 1 root Root 4 Nov 20 10:40 file2 -> file in the /tmp directory, create a file named file, create a hard link file1, soft link file2. Observe the output file of ls -il and the inode of file1 is the same, So it can be said that a hard link is a pointer to the same inode, and the system does not redistribute the inode for him. File2 points to file, indicating that file2 is a soft link, and the inode number also changes. If the original file is deleted, the hard link continues to be used, but the soft link is invalid. This is similar to the shortcut under win. What needs to be emphasized here is that the content of the third field in the output of the ls -l command, this is the number of inode links, as long as it is not 0, for hard links, the file will always exist, whether you delete the source file or before Generated hard links.
Copyright © Windows knowledge All Rights Reserved