Pay homage to the article written by the big man .songguojun
https://www.cnblogs.com/songgj/p/9115954.html
stay linux There are files in the system that are linked , Can be used for file sharing . There are two ways to link , One is hard link (Hard Link), The other is soft link or symbolic link (Symbolic Link).
Hard links
Hard linking refers to linking through index nodes . stay Linux In the file system , A file stored on a partition is assigned a number regardless of its type , This number is called the inode number (Inode
Index) perhaps Inode, It is the unique identification of a file or directory in a file system , The actual data of the file is placed in the data area (data block), It stores important parameter information of the file , That's metadata (metadata), Such as creation time 、 Modification time 、 file size 、 Belong to 、 The user group to which it belongs 、 read-write permission 、 Where the data is block Number, etc. , As shown in the figure below .
This is from the Internet
stay Linux In the system , Multiple filenames point to the same inode (Inode) It's normal and allowed . Generally, this kind of link is called hard link . One of the functions of hard links is to allow a file to have multiple valid pathnames , This allows users to create hard links to important files , To prevent “ False deletion ” Source data ( A lot of hardware , Such as netapp The snapshot function in storage applies this principle , Adding a snapshot adds a hard link 》. However, hard links can only be made between files in the same file system , Cannot create directory . The reason why files have hard links will prevent data from being deleted by mistake , Because the principle of the file system is , As long as the file's inode has more than one link ( Only the point of the file is deleted ), Deleting only one of the links does not affect the inode itself and other links ( The entity of the data is not deleted ), Only when the last link is deleted , At this point, if there is new data to be stored on the disk , The data block of the deleted file and the link to the directory will be released , Space is temporarily covered with new data .
Soft link
Soft link ( Also called symbolic link ), Be similar to windows System Shortcuts , Unlike hard links , Soft link is a common file , It's just that the data block content is a little special , The content stored in the file user data block is the point of the pathname of another file , In this way, you can quickly locate the source file entity that the soft link points to . Soft links can create files or directories .
Soft link function :
It is convenient for document management , For example, link a file under a complex path to a simple path to facilitate user access .
Save space and solve the problem of space shortage , A file system has run out of space , But now you have to create a new directory under the file system and store a lot of files , You can link a directory from another file system with more free space to that file system .
Deleting a soft link does not affect the file being pointed to , But if the original file pointed to is deleted , Then the related soft link becomes a dead link .
Features of soft link and hard link :
Soft link :
1. Soft links are the form of paths that hold another file .
2. Soft links can Cross file system , Hard links cannot .
3. Soft link can link to a file name that does not exist , Hard links must have source files .
4. Soft links can link directories .
Hard links :
- Hard links , In the form of a copy of a document . But it doesn't take up the actual space .
- Hard links to directories are not allowed .
- Hard links can only be created in the same file system .
- Deleting one of the hard link files does not affect the others to have the same inode Document No .
Neither hard link nor soft link will copy the original file , It only takes up a very small amount of disk space .
linux The system can be used ln Command to create a link file .
ln Command format :
ln [ Parameters ] [ Source file or directory ] [ Target file or directory ]
main parameter :
-i Interactive mode , Prompt the user whether to overwrite if the file exists .
-s Soft link ( A symbolic link ).
-d Allow super users to make hard links to directories .
-b Delete , Overwrite previously established links
Soft link ( A symbolic link ) ln -s source target
Hard links ( Entity link )ln source target
Case study :
Create hard links to files
Copy code
[[email protected] tmp]# mkdir dirIn Create a test directory
[[email protected] tmp]# cd dirIn/
[[email protected] dirIn]# touch infile
[[email protected] dirIn]# ll
Total usage 0
-rw-r--r-- 1 root root 0 5 month 31 14:53 infile
[[email protected] dirIn]# ln infile infile_hard_file By default, without parameters ,ln The command creates a hard link .
[[email protected] dirIn]# ll
Total usage 0
-rw-r--r-- 2 root root 0 5 month 31 14:53 infile
-rw-r--r-- 2 root root 0 5 month 31 14:53 infile_hard_file
Copy code
Create soft links to files
[[email protected] dirIn]# ln -s infile infile_soft_file Create soft links with s Parameters
[[email protected] dirIn]# ls -l
Total usage 0
-rw-r--r-- 2 root root 0 5 month 31 14:53 infile
-rw-r--r-- 2 root root 0 5 month 31 14:53 infile_hard_file
lrwxrwxrwx 1 root root 6 5 month 31 15:03 infile_soft_file -> infile Soft link
We know that hard links are the same inode Only files with different file names , use ls Command view .
[[email protected] dirIn]# ls -li
1177358 -rw-r--r-- 2 root root 0 5 month 31 14:53 infile
1177358 -rw-r--r-- 2 root root 0 5 month 31 14:53 infile_hard_file
Source files and hard link files above inode Same number ( The first column is inode Number ), Point to the same inode .
1177363 lrwxrwxrwx 1 root root 6 5 month 31 15:03 infile_soft_file -> infile Soft link is a link file , There's a... In front of the file mode section l Letter , It's a link file .
Hard link cannot point to directory
[[email protected] dirIn]# ln dir infile dir It's a catalog
ln: "dir": Hard links to directories are not allowed
Soft links can
[[email protected] dirIn]# ll -i
201884844 drwxr-xr-x 2 root root 19 5 month 31 15:21 dir
1177358 -rw-r--r-- 3 root root 0 5 month 31 14:53 infile
1177358 -rw-r--r-- 3 root root 0 5 month 31 14:53 infile_hard_file
1177363 lrwxrwxrwx 1 root root 6 5 month 31 15:03 infile_soft_file -> infile
1177365 lrwxrwxrwx 1 root root 3 5 month 31 15:24 infile_soft_file_name -> dir
Delete file test :
Copy code
[[email protected] dirIn]# echo "this a file" > infile Add data to the file
[[email protected] dirIn]# cat infile View file contents
this a file
[[email protected] dirIn]# cat infile_hard_file infile_soft_file Check the content of soft link and hard link file
this a file
this a file
[[email protected] dirIn]# rm -f infile Delete source file
[[email protected] dirIn]# ls -lrti
1177363 lrwxrwxrwx 1 root root 6 5 month 31 15:03 infile_soft_file -> infile
201884844 drwxr-xr-x 2 root root 19 5 month 31 15:21 dir
1177365 lrwxrwxrwx 1 root root 3 5 month 31 15:24 infile_soft_file_name -> dir
1177358 -rw-r--r-- 2 root root 12 5 month 31 15:27 infile_hard_file
[[email protected] dirIn]# cat infile_hard_file Hard link file exists The content is the same as the source file .
reason : Although deleting the source file , But there are still hard links to the source file inode
node , So it won't be released, deleted ,
This is just to delete infile To inode Links to nodes .
this a file
[[email protected] dirIn]# cat infile_soft_file Soft link files no longer exist , The directory also shows this file ,
That is, the soft link file is invalid .
cat: infile_soft_file: There is no file or directory
Copy code
Delete soft links
[[email protected] dirIn]# rm -f infile_soft_file Just delete this soft link file
[[email protected] dirIn]# ll
drwxr-xr-x 2 root root 19 5 month 31 15:21 dir
-rw-r--r-- 2 root root 12 5 month 31 15:27 infile_hard_file
lrwxrwxrwx 1 root root 3 5 month 31 15:24 infile_soft_file_name -> dir