The difference between hard links and symbolic links

  
        The first thing to understand is that in a Linux system, the kernel assigns an inode (index node) to each newly created file, and each file has a unique inode number. The file attributes are stored in the index node. When the file is accessed, the index node is copied to the memory to achieve quick access to the file. A link is a way to establish a connection between a shared file and several directory entries of the user accessing it. Linux includes two kinds of links: Hard Link and Soft Link. Soft links are also called Symbolic links. A hard link hard link is a pointer to a file index node. The system does not reassign the inode to it. You can use the :ln command to create a hard link. Syntax: ln [options] existingfile newfileln[options] existingfile-list directory usage: First: create a hard link for "existingfile", the file name is "newfile". Second: in the "directory" directory, Create a hard link with the same name for all files contained in "existingfile-list". Commonly used optional [options] –f Create links regardless of whether "newfile" exists or not. -n If "newfile" already exists, no link will be created. Here are some examples: $ ls –il13058 -rwx - - - - - - 1 longcheng longcheng 48 August 5 16:38 file113059 -rwx - - - - - - 1 longcheng longcheng 57 August 5 16:40 file2$ ln File2 file2hard$ ls –il13058 -rwx - - - - - - 1 longcheng longcheng 48 August 5 16:38 file113059 -rwx - - - - - - 2 longcheng longcheng 57 August 5 16:40 file213059 -rwx - - - - - - 2 longcheng longcheng 57 August 5 16:40 file2hard Note that the number of links displayed by file1 is 1 before the link is created. After the link is created (1) the number of links between file1 and file1hard becomes 2; (2) file1 and File1hard is the same in inode number (3) The file size displayed by file1 and file1hard is also the same. It can be seen that the operation of the ln command results: file1 and file1hard are two names of the same file, they have the same index node number and file attributes, and the hard link of the file1 is created, that is, the file index node of file1 is in the current directory. Create a new pointer. As shown below, you can delete any one of them, such as rm file2, only one pointer will be deleted at a time, and the number of links will be reduced by one. Only the pointer to the file content, that is, the number of links is reduced to 0, the kernel will put The contents of the file are deleted from the disk. The current directory logical structure: (sorry map is not shown). It is also possible to create hard links to files in different directories but in the same file system. Let file1 and file2 be in the directory /home/longcheng/dir1. The following command creates a hard link to file2 in /home/longcheng. Ln file2 /home/longcheng/file2hard The following program is to create all the files in the dir1 directory, create a hard link in the directory dir2 $mkdir dir2$ln /home/longcheng/dir1/* /home/longcheng/dir2 If you use ln –f existingfile newfile, if newfile already exists, no matter what file the original newfile is, only the current user has write access to it, newfile becomes a hard link file of exisitngfile. Although hard links save space, it is also the traditional way to integrate file systems in Linux systems, but there are some shortcomings: (1) It is not possible to establish links between files of different file systems. (2) Only superusers can create hard links for directories. . Although many trees say that the root user can create, but the author found in the learning process that even the root user can not create, my system is Redhat, the kernel 2.4, 2.6 have tried, in other systems do not know whether it is possible. In fact, under ubuntu, switching to superusers also makes it impossible to create hard links for directories. Note that the hard-wired file size is multiplied. Second, soft links (symbolic links) Symbolic links are indirect pointers to a file. A hard link points directly to the i-node of the file. Soft links overcome the shortcomings of hard links, without any file system limitations, any user can create symbolic links to directories. It is now more widely used, it has more flexibility, and can even link files across different machines and different networks. To create a soft link, just add the option –s after ln. Here is an example $ ls -il13058 -rwx - - - - - - 1 longcheng longcheng 48 August 5 16:38 file113059 -rwx - - - - - - 2 longcheng longcheng 57 August 5 16:40 file213059 -rwx - - - - - - 2 longcheng longcheng 57 August 5 16:40 file2hard$ln –s file1 file1soft$ls -il13058 -rwx - - - - - - 1 Longcheng longcheng 48 August 5 16:38 file113059 -rwx - - - - - - 2 longcheng longcheng 57 August 5 16:40 file213059 -rwx - - - - - - 2 longcheng longcheng 57 August 5 16:40 file2hard13061 lrwxrwxrwx 1 Longcheng longcheng 5 August 5 16:58 file1soft->gt1 The results from the above link can be seen as soft links and hard links, the difference is not only conceptually, but also different in implementation. The difference: the hard link original file & link file shares an inode number, indicating that they are the same file, and the soft link original file & link file has a different inode number, indicating that they are two different files; on the file attributes The soft link clearly writes the link file, and the hard link is not written, because in essence the hard link file and the original file are completely equal; the number of links is different, the number of links of the soft link does not increase; file size Is not the same, the size of the hard link file display is the same as the original file, this is emphasized, because it is equivalent, and the size of the soft link display here is different from the original file, the file1 size is 48B, and file1soft It is 5B, and the 5 inside is actually the size of "file1". In short, the establishment of a soft link is to create a new file. When accessing a linked file, the system will find that it is a linked file, which reads the linked file to find the file that is actually being accessed. Creating soft links between different systems and establishing links to directories is not an example here. Readers can try it on their own. I also learn in practice. Of course, soft links also have the disadvantage of hard links, because the link file contains the path information of the original file, so when the original file is moved from one directory to another, and then access the link file, the system can not find, and the hard link Without this flaw, how do you want to move it; and it has to allocate extra space for the system to create new index nodes and save the path to the original file. To add: you can view the link file through symlink, you can use man symlink to learn.
Copyright © Windows knowledge All Rights Reserved