Linked Files in Linux_Soft Links and Hard Links

1. Introduction to linked files

The "link file" in the Linux operating system is divided into hard link (hard link) and soft link (symbolic link). The essential difference between the two links is the inode. The following is a detailed introduction:

  • Hard link: When the system wants to read a file, it will read the inode information first, and then retrieve the data from the block field according to the information in the inode. The hard link is to directly establish an inode link to the block field where the file is placed, that is, when the hard link is performed, the content of the file does not change, but an inode pointing to the file is added, which does not occupy additional disk space. Hard links have two limitations:
  1. Cannot cross file systems, because different file systems have different inode tables;
  2. Directories cannot be linked.
  • Soft link: Different from hard link, soft link is to create a separate file. When reading the linked file, it will forward the read behavior to the file linked by the file. For example: now there is a file a, we have made a soft link file b, b points to a, when b is read, b will forward the read action to a, so that file a is read. When we delete file a, link file b will not be affected, but if we read b again, it will prompt that the file cannot be opened; however, when we delete b, it will not have any effect on file a.

 

Second, how to establish soft links and hard links

The format of the ln (link) command: ln [-s] [source file] [destination file] . The commonly used option of this command is -s. If the -s option is not added, a hard link is established, and the -s option is added to establish a soft link (you can remember it like this, s->soft (soft)) , for example:

Note: The du command in the above example is used to calculate the size of a file or directory, -k means the unit is KB, and 4 here means 4KB; the ll command is equivalent to ls -l.

At the beginning, there is only one passwd file under the directory, and the total size of the directory is 4KB. After making the hard link, although the size of the two files is 2364B, the total size of the directory does not change.

 

So let's try to delete the source file first, and then compare, for example:

In the above example, after deleting the source file passwd, the file size remains unchanged. Note that hard-linked files do not copy data blocks and take up additional disk space.

Let's look at another limitation of hard links - directories are not allowed to be hard links. example:

 

Soft link feature

First create a test directory 456, then copy the /etc/passwd file for testing, and then make a soft link file for it, for example:

In the above example, if the source file is deleted, the soft link file cannot be read , and the color changes when viewing with command ll.

 

Note that directories cannot be hard links, but can be soft links, for example:

 

Guess you like

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