Linux system, the distinction between "soft links" and "hard link" of

We know that the data file has a file name, which in  Linux  is divided into two parts: the user data (user data) and metadata (metadata). User data, i.e. the data block (data block), data block is a local log file contents are true; metadata are additional attributes file, such as file size, creation time, owner, and the like. In Linux, inode number metadata (inode is part of the metadata file but does not include the file name, inode number that is inode number) that uniquely identifies the file rather than the file name is. The file name is only for the convenience of people's memories and use the system to find the correct file or program data blocks through the inode number. And there is a way to quickly find the data element, and that is soft and hard links, let's look at it.

What is the link?

Links to say is actually a simple way of file-sharing is a concept in POSIX, major file systems support file link.

It is used to do?

You can link simply understood as the common Windows shortcut (or alias in OS X), Linux commonly used it to solve some of the library version of the problem, often add links to directory files to a deeper level more accessible directory. In these applications, we usually use soft links (also known as symbolic links).

content

Difference between soft and hard links are links?
Here we get into, to explore hard and soft links in the end what is the difference?

First, from the perspective of the use of speaking, there is no difference between the two are the same as the normal file access, support reading and writing, if it is, then the executable file can be executed directly.

That the difference between what it? In principle underlying.

To explain, we first create a file in your working directory, and then link to the file creation:

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

This is a plain text file.

Now we have created a common ground can no longer normal files. Then we create a hard link to it, and look at the current directory:

$ ln myfile hard
$ ls -li

25869085 -rw-r--r-- 2 unixzii staff 27 7 8 17:39 hard
25869085 -rw-r--r-- 2 unixzii staff 27 7 8 17:39 myfile

In the leftmost column of ls result, the value of the inode file, you can simply think of it as the C language pointer. It points to a block of physical hard drive, in fact, the file system maintains a reference count, as long as the file that points to this block, it will not disappear from the hard disk.

As you can see, these two documents as a file as the same inode value, all point to the same block.

Then we changed a bit hard link to the file you just created:

$ echo "New line" >> hard
$ cat myfile

This is a plain text file.
New line

You can see, these two documents is really a file. Here we look at soft links (that is, symbolic links), and it has no difference.

$ ln -s myfile soft
$ ls -li

25869085 -rw-r--r-- 2 unixzii staff 36 7 8 17:45 hard
25869085 -rw-r--r-- 2 unixzii staff 36 7 8 17:45 myfile
25869216 lrwxr-xr-x 1 unixzii staff 6 7 8 17:47 soft -> myfile

诶,你会发现,这个软链接的 inode 竟然不一样啊,并且它的文件属性上也有一个 l 的 flag,这就说明它与之前我们创建的两个文件根本不是一个类型。

下面我们试着删除 myfile 文件,然后分别输出软硬链接的文件内容:

$ rm myfile
$ cat hard

This is a plain text file.
New line
$ cat soft

cat: soft: No such file or directory

之前的硬链接没有丝毫地影响,因为它 inode 所指向的区块由于有一个硬链接在指向它,所以这个区块仍然有效,并且可以访问到。 然而软链接的 inode 所指向的内容实际上是保存了一个绝对路径,当用户访问这个文件时,系统会自动将其替换成其所指的文件路径,然而这个文件已经被删除了,所以自然就会显示无法找到该文件了。

为验证这一猜想,我们再向这个软链接写点东西:

$ echo "Something" >> soft
$ ls

hard myfile soft

We can see, just delete the file myfile actually appeared! This shows that when we write access soft links, the system automatically replaces its path as it represents the absolute path, and the path of direct access.

to sum up

Summarize
here we can sum up the fact:

Hard link: the file is no different from ordinary, inode block all point to the same file on the hard disk of
soft link: save the absolute path of the file it represents, is another document, a separate block on the hard disk, access when replacing the own path.


Guess you like

Origin blog.51cto.com/14414295/2481888