Difference [reprint] Linux under soft links and hard links

The difference between Linux under soft links and hard links

HTTP: // os.51cto.com/art/201911/605267.htm 

soft link file is just a small link deletion and other treatment did not affect the count of the original file to delete the source file link files across file immediately but finished mount point to connect. 

hard links increased innode reference count. when you delete a file as long as there is a link on there, but can not cross mount points are hard links 

flexible connection is ln -s hard link directly ln

 

 

In the Linux system, all files, however, to distinguish between different types of things, we have:

  • Trivial File
  • Directory files
  • Link file
  • Device file

In the previous article "? A which questions | epoll model used Nginx what" we discussed the concept of file descriptors:

File descriptor (file descriptor) is an index of the core has been opened in order to efficiently manage files created, which is a non-negative integer value (usually a small integer), used to refer to the file is opened, all perform I / O operating system calls are on file descriptor.

Interview | difference between Linux under soft links and hard links

For some use of Linux users, there will be similar to the following wording:

g++ lots_of_errors 2>&1 | head

Where 2> & 12 "standard error" is indicated, 1 is the "standard output", expressed with the middle & back of digital file descriptor instead of a file (or else all of the "standard error" to redirect to a file named 1 a).

This article will focus on expanded set forth for another interview:

Tell me What are the links? Soft links and hard links under Linux? What is the difference between them is it?

Link under Linux

As a user of Linux, the Linux system provides ln command to link to a file, we must have seen something like the following command:

Interview | difference between Linux under soft links and hard links

At this point if ls view files in the current directory, you will find:

Interview | difference between Linux under soft links and hard links

So what exactly is this foo.txt it?

This is a file link, file link is divided into hard link and soft link, by looking ln --help, you can see some important elements:

Interview | difference between Linux under soft links and hard links

ln command created by default is a hard link, if added to the -s parameter, a soft link is generated.

Hard links

Let's look at the hard link ln created by default, because the files under Linux is to identify the file by inode (Inode), the Linux file system, stored on disk partition files no matter what type it gave assign a number called an inode number (inode number).

Interview | difference between Linux under soft links and hard links

In Linux, a plurality of point to the same inode file name exists, it is hard-wired connection means carried by the index node, that is, each is a hard link to the file corresponding area.

We are here to create a file foo.txt then create a hard link to see it:

Interview | difference between Linux under soft links and hard links

6817859 is in front of the inode file, you can simply think of it as the C language pointer 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, where we will find these two files have the same inode, the contents will be found by looking at the file is the same file:

Interview | difference between Linux under soft links and hard links

The effect is hard link allows a file to have more than one valid path name, so users can create hard links to important documents, to prevent "accidental deletion" function, because the index node directory should have more than one connection, we assume foo.txt delete the original file:

Interview | difference between Linux under soft links and hard links

At this time, the contents of the file still exists, so only delete a connection does not affect the inode itself and other connections, only when the last link is deleted, the connection data block files and directories will be released, that is to say, the file will not actually be deleted.

Soft links

Interview | difference between Linux under soft links and hard links

Soft link called a symbolic link, the file contains another file path name, for example in the figure above, foo.txt is soft connection bar.txt of bar.txt is the actual file, foo.txt included for record of the inode bar.txt.

Soft connection can be any file or directory, you can link files in different file systems, at the time of the symbol files for read or write operations, the system will automatically put the operations into the operation of the source file, but delete the link file, the system just delete the link file, without deleting the source file itself, which is similar to a shortcut in the Windows operating system.

Difference between soft and hard links link

With the above in the knowledge that we can briefly answer the question of the interview:

What is the difference between soft and hard links are links?

Let's summarize:

Interview | difference between Linux under soft links and hard links

After possesses the knowledge linked aspects, as well as the following related questions can also be prepared to face them together:

  • Linux file system which
  • What Linux file types
  • Interprocess communication between users, which has several major ways
  • The concept and interrupt system calls

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11776661.html