In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
What is the difference between soft links and hard links in Linux system? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
We know that all files have file names and data, which is divided into two parts on Linux: user data (user data) and metadata (metadata). User data, namely file data block (data block), is the place where the real content of the file is recorded, while metadata is the additional attributes of the file, such as file size, creation time, owner, and so on. In Linux, the inode number in the metadata (inode is part of the file metadata but does not contain the file name, the inode number is the index node number) is the unique identity of the file rather than the file name. The file name is only for the convenience of people's memory and use, the system or program uses the inode number to find the correct file data block. And there is a way to quickly find data elements, that is soft and hard links, let's take a look at it.
What is a link?
To put it simply, linking is actually a way of file sharing and is a concept in POSIX. Mainstream file systems support linked files.
What is it for?
You can simply understand the link as a common shortcut in Windows (or a stand-in in OS X), which is often used in Linux to solve some library version problems, and usually links some files with a deeper directory to a more accessible directory. For these purposes, we usually use soft links (also known as symbolic links).
Content
What is the difference between soft links and hard links?
Let's get to the point and discuss the difference between soft and hard links.
First of all, from the point of view of use, there is no difference between the two, both are the same as the normal file access, support read and write, if it is an executable file can also be executed directly.
So what's the difference? At the bottom of the principle.
To explain clearly, we first create a file in one of our working directories, and then create a link to the file:
Touch myfile & & echo "This is a plain text file." > myfile$ cat myfileThis is a plain text file.
Now we have created a file that can no longer be ordinary. Then we create a hard link to it and look at the current directory:
$ln myfile hard$ ls-li25869085-rw-r--r-- 2 unixzii staff 27 7 8 17:39 hard25869085-rw-r--r-- 2 unixzii staff 27 7 8 17:39 myfile
In the leftmost column of the ls result is the inode value of the file, which you can simply think of as a pointer in the C language. It points to a block of the physical hard disk. In fact, the file system maintains a reference count, and as long as there is a file pointing to this block, it will not disappear from the hard disk.
As you can see, these two files are just like one file, with the same inode value and both pointing to the same block.
Then let's modify the hard link file we just created:
$echo "New line" > > hard$ cat myfileThis is a plain text file.New line
As you can see, these two files are really the same file. Let's take a look at the difference between soft links (that is, symbolic links) and it.
$ln-s myfile soft$ ls-li25869085-rw-r--r-- 2 unixzii staff 36 7 8 17:45 hard25869085-rw-r--r-- 2 unixzii staff 36 7 8 17:45 myfile25869216 lrwxr-xr-x 1 unixzii staff 6 7 8 17:47 soft-> myfile
Ah, you will find that the inode of this soft link is different, and its file attribute also has an l flag, which means it is not the same type as the two files we created before.
Let's try to delete the myfile file, and then output the contents of the soft and hard link file respectively:
$rm myfile$ cat hardThis is a plain text file.New line$ cat softcat: soft: No such file or directory
The previous hard link has no effect at all, because the block it inode points to is still valid and accessible because it has a hard link pointing to it. However, the content pointed to by the soft link inode actually saves an absolute path, and when the user accesses the file, the system will automatically replace it with the file path it refers to. However, the file has been deleted, so it will naturally show that the file cannot be found.
To test this conjecture, let's write something to this soft link:
$echo "Something" > > soft$ lshard myfile soft
You can see that the myfile file just deleted has appeared again! This means that when we write an access soft link, the system automatically replaces its path with the absolute path it represents and accesses that path directly.
At this point, we can actually summarize:
Hard links: no different from ordinary files, inode all point to the blocks of the same file on the hard disk
Soft link: saves the absolute path of the file it represents, is another kind of file, has a separate block on the hard disk, and replaces its own path when accessed.
The answer to the question about what is the difference between soft link and hard link in Linux system is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.