Linux-------soft link and hard link

The following is an article written by someone else, with a link at the end!

A syntax for establishing soft links and hard links

Soft link: ln -s source destination file
hard link: ln source destination file
source file: that to whom you want to establish a link

2. What are soft links and hard links

1. Soft links can be understood as shortcuts. It has the same function as shortcuts under windows.
2. Hard link is equal to cp -p plus synchronous update.

For convenience, I created test folder in the root partition (/), and then create a file jys
Write picture description here
the soft links and hard links:
Write picture description here

Difference: The size and creation time of the soft link file is different from the source file . The soft link file only maintains the pointing relationship from the soft link to the source file (as can be seen from jys.soft->jys), it is not the content of the source file, and the size is not as easy to understand.
The size of the hard link file and the source file are the same as the creation time . The content of the hard link file is exactly the same as the content of the source file, which is equivalent to a copy.
Write picture description here

However, the time to create the file for a simple copy should be the time when the file is copied, and it will certainly not be the same as the time when the source file is created like a hard link. You are right.
Write picture description here
But just add an option -p, the same time the
Write picture description here
Write picture description here
so cp -p file is not the equivalent of a hard link to it? In fact, if the content of the source file is modified, the hard-linked file will be updated and modified synchronously to always keep the content of the source file the same, while the copied file cannot achieve this.
Write picture description here
Therefore, the hard link is equal to cp -p plus synchronous update.
Soft links are like shortcuts, which are convenient for us to open source files. This is a deep experience in windows. What applications are there for hard links?
In a multi-user operating system, you write a script, program, etc., but it is not completed. After saving, wait until there is time to continue writing next time, but other users may treat your unfinished things as rubbish and clean up. At this time, you Make a hard link to your program, script, etc., and use the synchronous update of the hard link, so that someone else deletes your source file by mistake.

Third, delete the impact of multiple soft links and hard links in the source file

Write picture description here
Check the soft link file, the file you checked does not exist. Just like windows, when you delete source files, shortcuts won’t work. But if the source file is deleted, why can the hard-linked file still be viewed?
Here we want to briefly talk about the i-node. The i-node is the unique identification of files and directories. Each file and directory must have an i-node, otherwise the operating system will not be able to identify the file or system, just like a black account without a registered account. The linux operating system does not recognize some letters, like these jys, jys.hard operating systems do not know what it is.
Write picture description here
It can be seen that the hard link file and the source file i-node number are the same, and one i-node can correspond to multiple file names.
Write picture description here
As shown in the figure, jys is deleted, but the mapping relationship from 920586 to jys is deleted, and the mapping relationship between it and jys.hard is not affected. This figure also explains the synchronous update of hard links. For source file modifications, the operating system only recognizes i-nodes, so the operating system writes the modified content into all i-nodes with the same name and different files. At this point, I have a whim, if the hard link file is modified, will the source file be updated synchronously? Leave it to the reader to experiment for yourself!

The answer is yes.

This article is from here, please click!


Dividing line


ln command

Soft link: ln -s source file target file
Hard link: ln source file target file
Source file: to whom you want to establish a link

-f: If the target file exists, it will directly delete the target file and then create it.

Limitations of hard links:

  1. Cannot cross file system
  2. Can't link directory

(Soft connection does not have these restrictions.)

A hard link is just a new file name linked to an associated record of an inode number in a certain directory.

Symbolic link:

A symbolic link is to create an independent file, and this file will let the data read the file name of the file it is linked to, that is to say , the content of the linked file is that it will write the file name of the target file , so the size of the file It is the size of the file name of the target file, each letter occupies one byte.

How do symbolic links link to directories?

[root@A home]# ln -s /home/k /home/ab
[root@A home]# ls
ab k
[root@A home]# cd ab
[root@A ab]# ls
main.cpp
this command Indicates that a symbolic link with the name ab linked to the /home/k directory is created in the /home directory. Your access to the ab directory is actually to access the k directory

About the number of links to the catalog

In other words, when we create a directory, what is its default number of links? We know that creating an empty directory will exist. And... These two directories.
For example, when we create the /testing directory, there are basically three files /testing, /testing/. and /testing/..., then the number of links in the new directory is 2, and the number of links in the upper directory will increase 1. (It is affected by the above files)
Picture:

Guess you like

Origin blog.csdn.net/weixin_43743711/article/details/115250843