linux file link

My github, welcome to follow, share knowledge and technology

Linking: A method of establishing a link between a shared file and several directory entries of the user accessing it. There are two types of links in Linux: hard links and soft links. Soft links are also called symbolic links.

Let's first understand a few nouns:

index node (inode)

To understand links, we first have to understand a concept called an inode. In the Linux system, the kernel allocates an inode (index node) to each newly created file, and each file has a unique inode number. We can simply understand the inode as a pointer, which always points to the specific value of this file. storage location. The file attributes are stored in the index node. When accessing the file, the index node is copied to the memory, so as to realize the fast access of the file. The system locates each file by its inode (not the filename).

Hard Link

A hard link is a pointer that points to the file index node, and the system does not reallocate the inode for it. You can use the :ln command to create hard links.

quote

ln [options] existingfile newfile
ln [options] existingfile-list directory

Usage:
The first one creates a hard link for "existingfile", the file name is "newfile".
The second creates a hard link with the same name in the "directory" directory for all files contained in "existingfile-list".
Commonly used optional [options]: -f Create a link regardless of whether "newfile" exists or not. -n Do not create a link if "newfile" already exists.

Soft Link

A soft link, also known as a symbolic link, is a file that contains the pathname of another file. Can be any file or directory, and can link files from different file systems. It is similar to the shortcut under win.
You can use: ln -s command to establish a soft link.

quote

ln -s existingfile newfile
ln -s existingfile-list directory

Below we use an example to illustrate hard links and soft links.
There are now two files in the directory, one named file1 and one named file2.
quote

$ ls –il
1052671 -rw-r--r-- 1 Leanx Leanx 0 2010-03-29 13:46 file1
1052670 -rw-r--r-- 1 Leanx Leanx 0 2007-03-29 13:46 file2

First make a hard link to file1.

quote

$ ln file1 file1hard
$ls –il
1052671 -rw-r--r-- 2 Leanx Leanx 0 2010-03-29 13:46 file1
1052670 -rw-r--r-- 1 Leanx Leanx 0 2010-03-29 13:46 file12
1052671 -rw-r--r-- 2 Leanx Leanx 0 2010-03-29 13:46 file1hard

Here we note that before the link is created, the number of links displayed by file1 is 1. After the link is created

  • The number of links for both file1 and file1hard becomes 2.
  • The inode numbers of file1 and file1 are the same, both are 1052671.
  • The file sizes displayed by file1 and file1hard are also the same, both are 0B.

It can be seen that the operation result of the ln command: file1 and file1hard are two names of the same file, they have the same inode number and file attributes, and the hard link of the file file1 is established, that is, the file inode of file1 is in the current directory. Create a new pointer. You can delete any one of them, such as rm file1, only one pointer will be deleted at a time, and the number of links will be reduced by one at the same time. delete on disk.

Although hard links save space and are the traditional way for Linux systems to consolidate file systems, there are some disadvantages:

  • Hard links to directories are not allowed.
  • It is not possible to establish links between files on different file systems.

Then we make a soft link pointing to file2. The soft link overcomes the insufficiency of hard links. Without any file system restrictions, any user can create a symbolic link pointing to the directory. As a result, it is now more widely used, it has more flexibility, and it can even link files across different machines and different networks.

quote

$ ln -s file2 file2soft
$ ls –il
总用量 0
1052671 -rw-r--r-- 2 Leanx Leanx 0 2010-03-29 13:34 file1
1052670 -rw-r--r-- 1 Leanx Leanx 0 2010-03-29 13:35 file2
1053313 lrwxrwxrwx 1 Leanx Leanx 5 2010-03-29 13:45 file2soft -> file2
1052671 -rw-r--r-- 2 Leanx Leanx 0 2010-03-29 13:34 file1hard

From the results of the above link, it can be seen that the difference between soft links and hard links is not only in concept, but also completely different in implementation.
the difference:

  • The hard link original file/link file share an inode number, indicating that they are the same file, while the soft link original file/link file have different inode numbers, indicating that they are two different files;

  • On the file attribute, the soft link is clearly written as a link file, but the hard link is not written, because in essence, the hard link file and the original file are completely equal;

  • The number of links is different, and the number of soft links will not increase;

  • The file size is not the same, the size of the hard link file is the same as the original file. The size of the soft link displayed here is different from the original file. The size of file2 is 0B, while the size of file2soft is 5B.

    In short, to create a soft link is to create a new file. When accessing the linked file, the system will find that it is a linked file, and it reads the linked file to find the file to be accessed.

    Of course, soft links also have shortcomings that hard links do not have, because the linked file contains the path information of the original file, so when the original file is moved from one directory to another directory, and then the linked file is accessed, the system will not be able to find it, and the hard link will not be found. Without this defect, you can move as you want; and it requires the system to allocate extra space for creating new inodes and saving the path of the original file.

Reference link

The original source of the article

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949779&siteId=291194637