Linux system link---hard link

link concept

In the linux system, links can be divided into two types: one is a hard link (Hard Link), and the other is a soft link or a symbolic link (Symbolic Link or Soft Link). The ln command creates a link file. In the case of no parameters by default, the link created by executing the ln command is a hard link. If the link is created with -s it is a soft link.

hard link

Hard links refer to links through index nodes (inodes). In the linux (ext2, ext3, ext4) file system, the file stored in the disk partition will be assigned a number no matter what type it is. The number of the file.

In the linux filesystem, it is normal and allowed for multiple filenames to point to the same inode. A file in this case is called a hard link.

The hard link file is equivalent to another entry to the file. One of the effects of hard links is to allow a file to have multiple valid pathnames (multiple entries), so that users can establish hard links to important files to prevent "misdeletion" of source data (many hardware storage, such as netapp storage The snapshot function in the application applies this principle, and adding a snapshot adds a hard link).

Why does a file establish a hard link to prevent accidental deletion of data?

Because the principle of the file system (ext2) is that as long as the inode of the file has more than one hard link. Deleting only one of the hard links (that is, only deleting the link to the file) does not affect the inode itself and other links, that is, the entity of the data file is not deleted.

Only when the last link of the file is deleted, when there is new data to be stored on the hard disk or when the system does a disk check through something like fsck. The data blocks and directory links of the deleted files are released, and the space is occupied and overwritten by new data, and the data can no longer be retrieved.

hard link creation

ln 源文件 硬链接文件

Hard link knowledge summary

  • Multiple files with the same inode node number are hard-linked files to each other.
  • Delete the hard link file or delete any of the source files, the file entity is not deleted.
  • The file entity will only be deleted if the source file and all corresponding hard-linked files are deleted.
  • When all hard link files and source files are deleted, the new data will occupy the space of this file, or the deleted data will be reclaimed by the system when the disk is checked by fsck.
  • A hard-linked file is another entry point to a file.
  • You can prevent important files from being accidentally deleted by setting hard links to files.
  • The hard link can be created by executing the command ln 源文件 硬链接文件.
  • Hard linked files can be removed with the rm command.
  • For files that are not being called by the process, when the number of corresponding hard links is 0 (i_link), the file is deleted.

example

[root@ianLinux test]# cat ianfile
study linux.
  • 1
  • 2

write picture description here

write picture description here

Reprinted to https://blog.csdn.net/codetz/article/details/52414671

Guess you like

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