The difference between "soft link" and "Hard link" of LINUX

Source: Internet
Author: User

Today, I see a very interesting question on the understanding:
How to evaluate Microsoft senior Engineer obsessed with soft link this common concept of Linux?

Although it is well-known tearing forced the king has a certain tear force, but I still want to talk about the problem of the link in a simple way.

What is a link?

Simple link is actually a way of file sharing, is the concept of POSIX , the mainstream file system supports linked files.

What is it used for?

You can simply interpret the link as a common shortcut in Windows (or an alias in OS X), which is commonly used in Linux to solve problems with some library versions, and often links files with deeper directory hierarchies to a more accessible directory. For these purposes, we usually use soft links (also called Symbolic links ).

What is the difference between a soft link and a hard link?

Below we go to the point, to discuss the two kinds of soft and hard links in the end what is the difference?

First of all, from the perspective of use, there is no difference between the two, and the normal way to access the file, read-write support, if the executable file can also be directly executed.

What's the difference? In the underlying principle.

To explain this, we first create a file in our own working directory and then create a link to the file:

echo "This is a plain text file." > myfile$ cat myfileThis is a plain text file.

Now we have created an ordinary file that can no longer be normal. 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 result, the value of the ls file inode , you can simply think of it as a pointer in the C language. It points to a chunk of the physical hard disk, in fact the file system maintains a reference count, and it does not disappear from the hard disk as long as the file points to that chunk.

As you can see, these two files, like a file, have the inode same value, pointing to the same chunk.

Then we modify the hard link file that we just created:

echo "New line" >> hard$ cat myfileThis is a plain text file.New line

As you can see, these two files are indeed a file.
Let's look at the difference between a soft link (that is, a symbolic link) and what it does.

$ 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

Well, you will find that this soft link is inode not the same ah, and its file attributes also have a l flag, which means that it and the previous two files we created is not a type at all.

Below we try to delete the myfile file, and then output the contents of the soft and hard linked file separately:

$ rm myfile$ cat hardThis is a plain text file.New line
$ cat softcat: soft: No such file or directory

The previous hard link did not affect the slightest, because the inode chunk it points to has a hard link pointing to it, so the chunk is still valid and can be accessed.
However, the content of the soft link inode is actually to save an absolute path, when the user accesses the file, the system will automatically replace it with the file path it refers to, but the file has been deleted, so it will naturally show that the file cannot be found.

To verify this conjecture, let's write something about this soft link:

echo "Something" >> soft$ lshard   myfile soft

As you can see, the myfile file that you just deleted appears again! This means that when we write access to a soft link, the system automatically replaces its path with the absolute path it represents and accesses that path directly.

Summarize

Here we can actually summarize:

    • Hard links: No different from normal files, inode all point to the same file in the hard disk block
    • Soft Link: Save the absolute path of the file it represents, is another file, on the hard disk has a separate block, access to replace its own path.


Originating From: https://www.jianshu.com/p/dde6a01c4094

The difference between "soft link" and "Hard link" of LINUX

Related Article

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.