Soft and hard links in Linux

Source: Internet
Author: User
Tags inode usage

When it comes to soft and hard links in Linux, it's important to talk about the key parts of the Linux file system, Inode and block.

The first is the Inode, which first uses a graph to understand the status of the inode in the Linux file system:

File names, file attributes, and file data for files in Linux are stored separately. The file name is stored in the directory entry (dentry), the attributes of the files are stored in the inode, and the contents of the file, that is, the actual data of the file are stored in the block, and the directory is only inode. In general, the size of this inode is 128b,inode in the following:

  1.文件的字节数;  2.文件拥有者的Uid;  3.文件的Gid;  4.文件的读、写、执行权限(r,w,x);  5.文件的时间戳, ctime 指iNode上一次变动的时间,mtime指文件内容上一次变动 的时间,atime指文件上一次打开的时间;  6.链接数,即有多少文件名指向这个iNode;  7.文件对应的block的位置  可以用stat命令,查看某个文件或目录的iNode信息:The inode in the hard disk is limited, df-i can be used to view the Inode usage of the system: when the system accesses the file, it looks for the filename in the directory entry and the Inode mapping table, and then accesses the block of the file based on the block number and file size of the file in the Inode.Hard Links:In general, the file name and Inode are "one by one counterpart", as in the example above, the number of links to User_file.txt is 1, indicating that no other file name is linked to this inode. Look at the following example: The number of links in MYBPR is 2, indicating that there is a hard link, that is, there are other file names linked to this inode. Modifying the contents of a file with one of the filenames will have an impact on all two files. However, deleting a file name does not affect the normal access of another file name.Soft Links:In this link, two files (files A and B) have different file names and inode (in hard links, two files have different filenames, but the inode is the same), but the contents of one of the files (assuming file a) are stored in a block of another file (assuming file B) (the full directory, Including the file name), in which case, when accessing file a, the system automatically accesses the contents of File B's block in accordance with the contents of file A's block. At this point, if file B is deleted and then Access file A, the system will error, informing the user that the file does not exist. This guarantees a certain degree of security. Unlike hard links, soft links consume the inode of the system.the main differences between hard links and soft links are: (1)软链接可以跨文件系统,硬链接不可以。 (2)硬链接不管有多少个,都指向的是同一个iNode,会把iNode链接数增加as long as the number of links in the inode is not 0, the file persists, whether you delete the source file or the linked file. As long as therean existence, the file exists (in fact, there is no source file linked files, because they point to the same iNode) 当你修改源文件或者链接文件任何一个的时候 ,其他的文件都会做同步的修改。软链接不直接使 用iNode号作为文件指针,而是使用文件路径名作为指针(软链接跟源文件的iNode不一样)。所以 删除链接文件对源文件无影响,但是删除源文件,链接文件就会找不到要指向的文件 。软链接有 自己的iNode,并在磁盘上有一小片空间存放路径名. (3)软链接可以对一个不存在的文件名进行链接。 (4)软链接可以对目录进行链接,而硬链接不可以。PS:A blogger takes a shortcut in Windows to describe one of the soft and hard links, which I think is fraught ... I was so misled, until I read several blog posts and combine Linux private dishes, took a full two hours to figure out the concept of soft and hard links.

Soft and hard links in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.