In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the example analysis of linux hard link and soft link, the article is very detailed, has a certain reference value, interested friends must read it!
Preface
Recently, the front-end package manager pnpm is really too popular, a large number of articles have analyzed the principle of pnpm. After learning about it, I found that the entire architecture of pnpm is based on hard links and soft links, but I am rather vague about these two concepts, so I want to study them.
As we all know, everything in the Unix/Linux system is a file. As you can see, files are very important in Linux systems. Our usually more intuitive feeling for the file must be the file name and file content. But in the Linux file system, in addition to the file name and file content, there is a very important concept, that is, inode.
Inode
Wikipedia describes inode as follows:
The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data.File-system object attributes may include metadata (times of last change,access, modification), as well as owner and permission data.
A directory is a list of inodes with their assigned names. The list includes an entry for itself, its parent, and each of its children.
Inode is a data structure used to describe file system objects (such as files or folders) in a Unix-like file system. It stores various attributes of the file (meta-information such as the time of the last inode change, the time of the last visit, the time of the last modification, and permission information, etc.). A folder is a set of inode, including its own portal, the entry of the parent node, and all child nodes.
In fact, inode contains more than the above, specifically:
Number of bytes in the file
User ID of the file
Group ID of the file
Read, write and execute permissions of files
Timestamp: time when ctime,inode was last changed; mtime, when the content of the file was last changed; atime, when the file was last opened
The number of links, that is, how many file names point to this inode
Location of the file data block
In the ext2/ext3 file system used by Linux, different types of data are stored in different areas. The inode table composed of inode is stored in one location, and the file data blocks are stored in another location.
Inode does not contain a file name, which is stored in the structure of the folder information. The file name is equivalent to the alias of inode, which is easy for us to manage and remember. Linux system operates files through inode. When we modify files, the system finds the inode corresponding to the file name from the information structure of the folder, and then finds the corresponding hard disk location through the block address of the file data stored in inode for reading and writing operations.
Hard link
Generally speaking, inode has an one-to-one relationship with file names and file data, but we can use the shell command to point multiple file names to the same inode, which is called hard link.
Use the ln command to create hard links, such as
Ln test.txt test_hard.txt
Corresponds to the fs.link method of nodejs.
Before creating a hard link, test.txt can say:
After creating a hard link:
As you can see, test_hard.txt 's inode is the same as the source file test.txt, but now the number of links has changed to 2.
We can run ls-li to check it out.
The first column is inode number, and you can see that both files are 13029546, so both files use the same inode. The second column is the permission information, the fourth column is the owner, and the sixth column is the file content size. As you can see, the file created by the hard link is exactly the same as all the meta-information of the source file, except that the file name is different. The third column represents the number of links, and as you can see, the current number of links is 2.
Because the hard-linked file and the source file use the same inode and point to the same piece of file data, all information except the file name is the same. So these two files are equivalent and can be said to be hard link files to each other. Modify any file and you can see that the contents of another file will change synchronously.
Soft link
To be exact, it is called symbolic link (symbolic link), and it is generally called soft link (soft link). Instead of sharing an inode with hard links, soft links create a new inode and point to the source file. It is understandable that soft links are desktop shortcuts in windows systems.
The command to create a soft link is very similar to a hard link, with the-s parameter: ln-s:
Ln-s test.txt test_symbolic.txt
The fs.symlink method of the corresponding nodejs.
After creating a soft link:
The number of links to the source file inode is still 1, a new inode is created, and the soft link points to the source file.
Execute ls-li to take a look:
As you can see, the inode number of the soft link is different from that of the source file. The permission column begins with a lowercase L, which indicates the soft chain. The number of links is 1, and the size is 8 bytes. Yes, soft chain files are also available in size, but they are generally very small and are just a shortcut after all.
Compare file renaming or file movement
File renaming and file movement are both changes to the absolute path of the file to the Linux system. For hard links, file renaming or file movement will not change the link point, while for soft links, file renaming or file movement will break the link, and when the file content is modified through the soft link, a new inode will be created again, associated with the original file name and file data block.
File deletion
The rm command or nodejs's unlink actually subtracts the number of links from inode by 1. For the hard links mentioned above, deleting test_hard.txt makes the number of links in inode1 become 1. When the number of links becomes 0, the system will release the inode, and new files created later can use the inode number of the inode. There is no inode pointing to the file data block, so the file cannot be found. But in fact, the file data is still on the hard disk, so you can often see some tools on the Internet to help recover mistakenly deleted files. The number of soft link inode links is 1. Delete the soft link and the system releases the inode.
Link files and folders
Soft links can link files and folders, but hard links can only link files.
Create links for different file systems
Soft links can be created across different file systems, but hard links are not, because hard links share an inode, while different file systems have different inode table.
Application scene hard link
File backup: in order to prevent important files from being deleted by mistake, file backup is a good way, but copying files can lead to disk space consumption. Hard links can back up files without taking up disk space.
File sharing: when multiple people work together to maintain the same file, you can create a hard link in a private directory by means of hard links, so that everyone's changes can be synchronized to the source file, but avoid the problem of someone mistakenly deleting the file.
File classification: different file resources need to be classified, for example, if the classification of a movie is foreign or suspense, then we can create hard links in foreign folders and suspense folders respectively. in this way, you can avoid wasting disk space by repeatedly copying movies. Some people may say, isn't it okay to use soft links? Yes, but not so good. Because once the source file is moved or renamed, the soft link is invalid.
Soft link
Shortcuts: for files with deep paths, it is not easy to find them. Using soft links to create shortcuts on the desktop, you can quickly open and edit files.
Flexible switching program version: for programs that have multiple versions on the machine at the same time, you can quickly change the program version by changing the direction of the soft link. It is mentioned here that switching between python versions can be done.
Dynamic library version management: not very understand, you can see here for details.
Summary
Linux system manages files through inode. Inode stores information such as file bytes, file permissions, number of links, data block location and so on.
Hard links share inode with the source file, except for the file name, which is the same as the source file. You cannot create hard links to folders or files from different file systems.
Soft links are similar to windows shortcuts and have separate inode. You can create soft links to files in folders or different file systems.
Both hard and soft links modify the contents of the file are synchronized to the source file because they are essentially data block pointing to the source file.
The above is all the contents of this article "sample Analysis of linux hard links and soft links". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.