Linux getting started Tutorial: Soft links and hard links

Source: Internet
Author: User

Linux getting started Tutorial: Soft links and hard links

Summary

1. Differences between soft links and hard links:

We can intuitively feel it through an instance.

* Soft link:

(1) Here is the details of the/mnt/file:

Note that the inode Number of the leftmost file is 6294764. We will compare it with the link file later.

(2) create a soft link:

Ln-s/mnt/file/root/Desktop (-s indicates soft link)

Then enter the command ls-il/root/Desktop, and the/root/Desktop will display the following:

Well, pay attention to the following details:

1. The inode of the file is different from the previous one, and the file becomes 12329279 .;

2. Change the File Permission location to l, indicating that this is a link;

3. the file size also changed from 65 to 9. After editing the/root/Desktop/file, I found that the file size is still 9. the file points to/mnt/file.

4. The most critical point is that the number of inode links is 1, not 2.

 

(3) Monitor the source file content:

Watch cat/mnt/file, originally:

Edit the file on/root/Desktop:

Vim/root/Desktop/file. After Entering vim, it is found that the content is the same as/mnt/file.

When we edit and save the file on/root/Desktop, we find that the content of/mnt/file also changes.

 

* Hard link

Create a hard link:

We can understand the hard link as a pointer, but the system will not re-allocate the inode for it ,:

The inode is the same as the source file. The file size is the same, but there is a small change in details: the number of links has changed to 2.

We can understand the operation result of the ln command that the two files are actually two different names of the same file, as if the two pointers point to the same region. They have the same inode number and file attributes. Creating a hard link is to create a new pointer. If we want to delete this file, we must delete all the links and change the number of inode to 0, in this case, the kernel will delete the file from the device.

 

Hard links have the following restrictions:

(1) hard links cannot be established between different file systems;

(2) Only Super Users can create a link for the directory. I switched to the normal user student and executed the hard link. An error occurred:

* Differences between soft links and hard links: (this part is excerpted from others, because I think the summary is comprehensive ):

A public inode number is used to hard link the original file and the linked file, indicating that they are the same file, while soft link to the original file and the linked file has different inode numbers, indicates that they are two different files;

The soft link in the file property clearly indicates that it is a link file, but the hard link is not written, because in essence the hard link file is completely equal to the original file;

The number of soft links is not increased;

The file size is different. The displayed size of the hard link file is the same as that of the original file. This is emphasized because it is equivalent, the size of the soft link is different from that of the original file. The size of file1 is 48B, and that of file1soft is 5B. The actual size of file1 is used.

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.

Linux hard links and soft links

About Soft links and hard links in Linux

RHCE file management-hard links and soft links

This article permanently updates the link address:

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.