Hard link and soft link in Linux system

Sanshui longevity cocoa 2020-11-10 18:08:05
hard link soft link linux


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 :

  1. Hard links , In the form of a copy of a document . But it doesn't take up the actual space .
  2. Hard links to directories are not allowed .
  3. Hard links can only be created in the same file system .
  4. 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
[root@localhost tmp]# mkdir dirIn Create a test directory
[root@localhost tmp]# cd dirIn/
[root@localhost dirIn]# touch infile
[root@localhost dirIn]# ll
Total usage 0
-rw-r--r-- 1 root root 0 5 month 31 14:53 infile
[root@localhost dirIn]# ln infile infile_hard_file By default, without parameters ,ln The command creates a hard link .
[root@localhost 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

[root@localhost dirIn]# ln -s infile infile_soft_file Create soft links with s Parameters
[root@localhost 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 .
[root@localhost 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

[root@localhost dirIn]# ln dir infile dir It's a catalog
ln: "dir": Hard links to directories are not allowed

Soft links can
[root@localhost 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
[root@localhost dirIn]# echo "this a file" > infile Add data to the file
[root@localhost dirIn]# cat infile View file contents
this a file
[root@localhost dirIn]# cat infile_hard_file infile_soft_file Check the content of soft link and hard link file
this a file
this a file
[root@localhost dirIn]# rm -f infile Delete source file
[root@localhost 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
[root@localhost 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
[root@localhost 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
[root@localhost dirIn]# rm -f infile_soft_file Just delete this soft link file
[root@localhost 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

版权声明
本文为[Sanshui longevity cocoa]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云