In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "the difference between linux hard links and soft links". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In short:
Soft links are equivalent to shortcuts in windows
The hard link is equivalent to a disaster recovery system, and the data is stored in two places. Unlike replication, there is a synchronization mechanism between the two places. One data change will be synchronized to another place in real time. If the other data is deleted, it will not affect the data in the other place.
The following is a detailed introduction:
There are two different types of links, soft links and hard links. Modify one of them so that the hard connection points to the node (inode) and the soft connection points to the path (path)
Soft link file
Soft links are also called symbolic links, and this file contains the pathname of another file. It can be any file or directory, and you can link files from different file systems. It is similar to the shortcut under win. Linked files can even link files that do not exist, which creates a problem commonly known as "chain breakage" (or "phenomenon"), and linked files can even link themselves in a loop. Similar to recursion in programming languages.
Command format:
Code: ln [- s] source_path target_path hard link file
The info ln command tells you that hard links are another name for existing files, and the commands for hard links are:
Code: ln-d existfile newfile / / if no parameters are added, the default is hard link.
Hard link file
Hard-linked files have two restrictions:
1. Hard links to directories are not allowed
2. Links can only be created between files in the same file system.
When you read, write and delete a hard link file, the result is the same as the soft link. But if we delete the source file of the hard-linked file, the hard-linked file still exists and retains the original content. At this point, the system "forgets" that it used to be a hard-linked file. And treat him as an ordinary document. Modify one of them, and the file connected to it is repaired at the same time.
Change.
Code:
$cp / etc/httpd/conf/httpd.conf / usr/sam
$ln httpd.conf httpd1.conf (hard link)
$ln-s httpd.conf httpd2.conf (soft link)
The first is hard link, the second is soft link.
Code:
$ls-li / / View the inode of a file or directory through the-I parameter of the ls command and the files with the same value of inode. their relationship is hard-linked to each other.
Code:
Total dosage 80
1077669-rw-r--r-- 2 sam adm 34890 October 31 00:57 httpd1.conf (number of connections changed to 2)
1077668 lrwxrwxrwx 1 sam adm 10 October 31 00:58 httpd2.conf-> httpd.conf (similar to Win shortcut)
1077669-rw-r--r-- 2 sam adm 34890 October 31 00:57 httpd.conf (number of connections changed to 2)
As you can see, with ls-li, the soft connection only produces a 10-byte shortcut, while the hard connection actually makes a copy. The front inode hard link is the same as the source file
Soft links are different. Edit the http1.conf and you can see that the httpd.conf has changed the same way.
Code:
$rm httpd.conf
Now delete the linked source file to compare the differences
Code:
$ls-l
Total dosage 44
Drw-r--r-- 2 sam adm 4096 October 30 20:14 file6
-rw-r--r-- 1 sam adm 34890 October 31 00:57 httpd1.conf
Lrwxrwxrwx 1 sam adm 10 October 31 00:58 httpd2.conf-> httpd.conf
Found that httpd2.conf actually no longer exists, is a broken chain, and httpd1.conf has become a common file.
Detailed example analysis (screenshot):
View all files and paths associated with hard link ln
Ls-I myInfo.txt
3814056 myInfo.txt
Find /-inum 3814056
/ home/homer/me/myInfo.txt
/ home/homer/me/.me/myInfo.txt_ln
/ home/homer/bin/myInfo.txt
A linked file
There are two ways to link, soft link and hard link.
1 soft link file
Soft links are also called symbolic links, and this file contains the pathname of another file. It can be any file or directory, and you can link files from different file systems.
Linked files can even link files that do not exist, which creates a problem commonly known as "chain breakage" (or "phenomenon"), and linked files can even link themselves in a loop. Similar to recursion in programming languages.
A soft connection can be generated with the ln-s command, as follows:
[root@linux236 test] # ln-s source_file softlink_file
When you read or write to the symbol file, the system automatically converts the operation to the source file, but when you delete the linked file, the system only deletes the linked file, not the source file itself.
2 hard link file
The info ln command tells you that hard links are another name for existing files (A "hard link" is another name for an existing file), which is somewhat confusing. The command for hard connection is
Ln-d existfile newfile
Hard-linked files have two restrictions
1) hard links to directories are not allowed
2) links can only be created between files in the same file system.
When you read, write and delete a hard link file, the result is the same as the soft link. But if we delete the source file of the hard-link file, the hard-link file still exists and the desired content is retained.
At this point, the system "forgets" that it used to be a hard-linked file. And treat him as an ordinary document.
The difference between the two
A hard connection is a connection made through an index node. In Linux's file system, files saved in disk partitions, regardless of type, are assigned a number called index node number (Inode Index).
In Linux, it is possible to have multiple file names pointing to the same Inode. Generally speaking, this kind of connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathnames, so that users can establish a hard connection to important files to prevent "accidental deletion". The reason is as mentioned above, because there is more than one connection to the index node of the directory. Deleting only one connection does not affect the Inode itself and other connections. Only when the last connection is deleted will the connection of the file's data block and directory be released. In other words, the file will really be deleted.
Soft-link files are a bit like Windows shortcuts. It is actually a kind of special file. In symbolic links, a file is actually a text file that contains information about the location of another file.
Soft links also have the disadvantage that hard links do not have, because the link file contains the path information of the original file, so when the original file is moved from one directory to another directory, and then access the link file, the system cannot find it; and it requires the system to allocate additional space to build new index nodes and save the path of the original file.
The hard link does not have the above defects, you can move it as much as you want.
Soft links are equivalent to shortcuts in windows. The hard link is equivalent to a disaster recovery system, and the data is stored in two places. Unlike replication, there is a synchronization mechanism between the two places. One data change will be synchronized to another place in real time. In addition, if one data is deleted, it will not affect the data in the other place.
Detailed introduction:
The hard link points to the node (inode), which is another name of the existing file. Modify one of them and modify it at the same time as the connected file; when you read, write and delete the hard link file, the effect is the same as the soft link. But if we delete the source file of the hard-linked file, the hard-linked file still exists and retains the original content. At this point, the system "forgets" that it used to be a hard-linked file and treats it as an ordinary file. Hard-link files have two restrictions: 1, hard links are not allowed to be created for directories, and 2, links can only be created between files in the same file system.
Soft links point to paths, also known as symbolic links, and this file contains another file whose path name can be any file or directory, and can also link files from different file systems, similar to shortcuts under win. Soft-linked files can even link files that do not exist, which creates a problem commonly known as "chain breakage" (or "phenomenon"), and linked files can even link themselves in a loop, similar to recursion in programming languages.
Example:
Ln exitfile newfile
Ln-s exitfile newfile
The newfile here is the name of the link created, the first one has no parameters, the default is hard link, and the second-s means soft link.
Ls-il can view attributes such as the inode number of a file, such as:
[root@server tmp] # touch file
[root@server tmp] # ln file file1
[root@server tmp] # ln-s file file2
[root@server tmp] # ls-il
Total 0
27127-rw-r--r--. 2 root root 0 Nov 20 10:40 file
27127-rw-r--r--. 2 root root 0 Nov 20 10:40 file1
27132 lrwxrwxrwx. 1 root root 4 Nov 20 10:40 file2-> file
In the / tmp directory, create a file named file, and then create a hard link file1 and a soft link file2. Observe the output of ls-il
The inode of file is the same as that of file1, so it can be said that a hard link is a pointer to the same inode, and the system does not reassign the inode for it. File2 points to file, indicating that file2 is a soft link, and the inode number has also changed.
If you delete the original file file, the hard link continues to be used, but the soft link is invalid. This is similar to shortcuts under win.
It should be emphasized here that the third field in the output of the ls-l command is the number of inode links. As long as it is not 0, the file will always exist for the hard link, whether you delete the source file or the hard link generated before.
This is the end of the content of "the difference between linux hard links and soft links". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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
Under apache2 bin directory-- start the service httpd-k restart
© 2024 shulou.com SLNews company. All rights reserved.