In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to create hard links and soft links in the Linux system". In the daily operation, I believe that many people have doubts about how to create hard links and soft links in the Linux system. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to create hard links and soft links in the Linux system". Next, please follow the editor to study!
First of all, make it clear that on Linux systems, the kernel assigns an Inode (index node) to each newly created file, and each file has a unique inode number. The file attributes are saved in the index node, and when accessing the file, the index node is copied to memory, so as to achieve fast access to the file.
A link is a way to establish a connection between sharing a file and several directory items of the user who accesses it. Linux includes two kinds of links: hard links (Hard Link) and soft links (Soft Link), which are also called symbolic links (Symbolic link).
I. hard links
To put it bluntly, the hard link is a pointer to the file index node, and the system does not reassign the inode to it. You can use the: ln command to establish a hard link. Syntax:
The code is as follows:
Ln [options] existingfile newfile
Ln [options] existingfile-list directory
Usage: first: create a hard link for "existingfile". The file name is "newfile". The second: in the "directory" directory, create a hard link with the same name for all files contained in "existingfile-list". Commonly used optional [options]-f creates a link regardless of whether "newfile" exists or not. -n if "newfile" already exists, no link is created.
Here are some examples:
The code is as follows:
$ls-il
13058-rwx-- 1 longcheng longcheng 48 August 5 16:38 file1
13059-rwx-- 1 longcheng longcheng 57 August 5 16:40 file2
The code is as follows:
$ln file2 file2hard
$ls-il
13058-rwx-- 1 longcheng longcheng 48 August 5 16:38 file1
13059-rwx-2 longcheng longcheng 57 August 5 16:40 file2
13059-rwx-2 longcheng longcheng 57 August 5 16:40 file2hard
Note that before creating links, the number of links displayed by file1 is 1, after creating links, (1) the number of links in file1 and file1hard is changed to 2; (2) file1 and file1hard in the inode number are the same (3) file1 and file1hard show the same file size. It can be seen that the operation results of the ln command: file1 and file1hard are two names of the same file, and they have the same index node number and file attributes. To establish a hard link to the file file1 is to establish a new pointer on the current directory for the file index node of file1.
The number of links is reduced by one at the same time, and only when all pointers to the contents of the file, that is, the number of links, are reduced to 0, will the kernel delete the contents of the file from disk. Current directory logical structure: (sorry the figure is not shown).
You can also establish hard links to files in different directories but in the same file system. Let file1 and file2 be in the directory / home/longcheng/dir1, and the following command establishes a hard link to file2 in / home/longcheng.
The code is as follows:
Ln file2 / home/longcheng/file2hard
The following program sets up a hard link to all the files in the dir1 directory in the directory dir2
The code is as follows:
$mkdir dir2
$ln / home/longcheng/dir1/* / home/longcheng/dir2
If you use ln-f existingfile newfile, if newfile already exists, newfile becomes a hard-linked file for exisitngfile, no matter what the original newfile file is, as long as the current user has write permission to it.
Although hard links save space and are the traditional way for Linux systems to integrate file systems, there are some shortcomings:
(1) links cannot be established between files of different file systems
(2) only superusers can create hard links for directories. Although many trees say that root users can create, but in the process of learning, I found that even root users can not create, my system is Redhat, kernel 2.4,2.6 have tried, I do not know if it is possible in other systems.
II. Soft links (symbolic links)
Soft links overcome the shortcomings of hard links, without any file system limitations, any user can create symbolic links to directories. As a result, it is now more widely used, it has more flexibility, and it can even link files across different machines and different networks.
To create a soft link, just add the option-s after ln. Here's an example.
The code is as follows:
$ls-il
13058-rwx-- 1 longcheng longcheng 48 August 5 16:38 file1
13059-rwx-2 longcheng longcheng 57 August 5 16:40 file2
13059-rwx-2 longcheng longcheng 57 August 5 16:40 file2hard
The code is as follows:
$ln-s file1 file1soft
$ls-il
13058-rwx-- 1 longcheng longcheng 48 August 5 16:38 file1
13059-rwx-2 longcheng longcheng 57 August 5 16:40 file2
13059-rwx-2 longcheng longcheng 57 August 5 16:40 file2hard
13061 lrwxrwxrwx 1 longcheng longcheng 5 August 5 16:58 file1soft- > file1
From the result of the link above, we can see that the difference between soft link and hard link is not only in concept, but also in implementation. Difference: the hard link original file-link file shares an inode number, indicating that they are the same file, while the soft link original file-link file has different inode numbers, indicating that they are two different files; in the file attributes, the soft link clearly indicates that it is the link file, while the hard link is not written, because in essence, the hard link file and the original file are completely equal. The number of links is not the same, the number of soft links will not increase; the file size is different, the size of the hard link file is the same as the original file, which is emphasized, because it is the same, and here the size of the soft link is different from the original file, file1 size is 48B, and file1soft is 5B, which is the 5 real "file1" size.
At this point, the study on "how to create hard links and soft links in the Linux system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.