Hard link and soft link

Source: Internet
Author: User
Tags symlink

// I find that I can't learn something in depth. The key point is that I didn't take notes. I remember all the things I learned for the time being. It will take a long time to forget them, now, I want to hear a good speech from a senior engineer and write a blog record about what I learned at ordinary times. Please do not laugh at heroes .......

First, you need to understand that in Linux, the kernel assigns an inode (index node) to each newly created file, and each file has a unique inode number. The file attributes are stored in the index node. when accessing the file, the index node is copied to the inner to implement fast file access. A link is a way to establish a connection between a shared file and several directory items of the users who access it. Linux contains two types of links: hard link and soft link. Soft link is also called symbolic link ). I. Hard link hard link is a pointer pointing to the file index node, and the system does not re-allocate inode for it. You can use the ln command to create a hard link Syntax: ln [Options] existingfile newfile ln [Options] existingfile-List Directory usage: the first method is to create a hard link for "existingfile, file Name: "newfile" Type 2: In the "directory" directory, create a hard link with the same name for all files contained in "existingfile-list, create link-N if "newfile" already exists, do not create a link. The following are some examples: $ LS-il13058-rwx--1 longcheng 48 August 8 16:38 file113059-rwx---1 longcheng 57 August 17 16:40 file2 $ ln file2 file2hard $ ls -il13058-rwx--1 longcheng 48 August 8 16:38 file113059-rwx---2 longcheng 57 August 7 16: 40File213059-rwx---2 longcheng 57 August 16: 40File2hard Note: before creating a link, the number of links displayed in file1 is 1. After the link is created, (1) the number of links in file1 and file1hard is changed to 2. (2) file1 and file1hard are the same in inode (3) the file size displayed by file1 and file1hard is the same. The result of LN command is as follows: file1 and file1hard are two names of the same file. They have the same index node number and file attributes and create a hard link to file1, creates a new pointer for the file index node of file1 in the current directory. For example, you can delete any of them, such as RM file2. Only one pointer is deleted at a time, and the number of links is reduced by one. Only pointers pointing to the file content are allowed, that is, when the number of links is reduced to 0, the kernel will delete the file content from the disk. You can also create hard links to files in different directories but in the same file system. Set file1 and file2 to the/home/longcheng/dir1 directory. Run the following command to create a hard link for file2 in/home/longcheng. in LN file2/home/longcheng/file2hard, all files in the dir1 directory are stored, create a hard link $ mkdir dir2 $ ln/home/longcheng/dir1/*/home/longcheng/dir2 in the dir2 directory. If ln-F existingfile newfile is used, no matter what the original newfile is, newfile becomes a hard-linked file of exisitngfile with the write permission of the current user. Although hard links save space, they are also the traditional way to integrate file systems in Linux systems, but there are some shortcomings: (1) You cannot establish links between files in different file systems (2) only Super Users can create hard links for directories. Although many trees say that root users can be created, I found that even root users cannot be created during the learning process. My system is RedHat and I have tried both kernel 2.4 and 2.6, in other systems, you do not know if it is OK. 2. Soft links (symbolic links) soft links overcome the shortcomings of hard links and do not have any restrictions on the file system. Any user can create symbolic links pointing to directories. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks. To create a soft link, add the option-s after ln, the following is an example of $ LS-il13058-rwx--1 longcheng 48 August 5 16:38 file113059-rwx---2 longcheng 57 August 5 16:40 file213059-rwx --2 longcheng 57 July 22, 16:40 file2hard $ ln-s file1 file1soft $ LS-il13058-rwx---1 longcheng 48 August 8 16:38 file113059-rwx --2 longcheng 57 16:40 file213059-rwx- --2 longcheng 57 16:40 file2hard13061 lrwxrwxrwx 1 longcheng 5 16:58 file1soft-> file1 the soft link and hard link can be seen from the above link results, the difference is not only in terms of concept, but also in terms of implementation. Difference: A Public inode number is used for hard-link files and linked files, indicating that they are the same file, while soft-link files and linked files have different inode numbers, it indicates that they are two different files. On the file attributes, the soft link clearly writes the link file, but the hard link is not written, in essence, hard-link files are completely equal to the original files. The number of links is different, and the number of soft-link links does not increase. The file size is different, the size of the hard link file is the same as that of the original file. This is emphasized because it is equivalent, and the size of the soft link is different from that of the original file, the size of file1 is 48B, while that of file1soft is 5B, where 5 is actually the size of "file1. In short, creating a soft link is to create a new file. When you access a linked file, the system will find that it is a linked file. It reads the linked file and finds the file to be accessed. Creating soft links between different systems and creating links to directories is not an example here. You can try it on your own. I am also learning it in practice. Of course, soft links also have the disadvantage that hard links do not exist. Because the link file contains the path information of the original file, when the original file is moved from one directory to another, access the link file, the system cannot find it ~~, Hard links do not have this defect, so you need to move them as much as you want (haha). In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path. Supplement: You can view the link file through symlink, and you can use man symlink to learn.

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.