In Linux, ln commands are provided to link files. File links are mainly divided into hard links and soft links.
Hard link: Because files in Linux are identified by inode, hard links can be considered as a pointer to the file index node, the system does not re-allocate inode for it. Each time a hard link is added, the number of links to the file is increased by 1.
You can use the ln command to create a hard link. Syntax:
Ln [Options] existingfile newfile <br/> ln [Options] existingfile-List Directory <br/>
Usage: the first method is to create a hard link for "existingfile" and the file name is "newfile ". Type 2: Create a hard link with the same name for all files contained in "existingfile-list" in the "directory" directory. Commonly used [Options]-F creates a link regardless of whether "newfile" exists or not. -N if "newfile" already exists, no link is created.
Example:
$ LS-IL <br/> 13058-rwx--1 longcheng 48 Aug 17 16:38 file1 <br/> 13059-rwx---1 longcheng 57 July 22, August 5 16:40 file2 <br/> $ ln file2 file2hard <br/> $ LS-IL <br/> 13058-rwx---1 longcheng 48 August 5 16:38 file1 <br/> 13059-rwx---2 longcheng 57 Aug 17 16:40 file2 <br/> 13059-rwx---2 longcheng 57 Aug 17 16:40 file2hard
Note: before creating a link, the number of links displayed in file1 is 1. After the link is created, (1) the number of links in file1 and file1hard is changed to 2. (2) file1 and file1hard are the same in inode (3) the file size displayed by file1 and file1hard is the same. The result of LN command is as follows: file1 and file1hard are two names of the same file. They have the same index node number and file attributes and create a hard link to file1, creates a new pointer for the file index node of file1 in the current directory. For example, you can delete any of them, such as RM file2. Only one pointer is deleted at a time, and the number of links is reduced by one. Only pointers pointing to the file content are allowed, that is, when the number of links is reduced to 0, the kernel will delete the file content from the disk.
Disadvantages:
Although hard links save space, they are also the traditional way to integrate file systems in Linux systems, but there are some shortcomings: (1) You cannot establish links between files in different file systems (2) only Super Users can create hard links for directories.
Soft link (symbolic link ):
Soft links overcome hard links without any restrictions on the file system. Any user can create symbolic links pointing to directories. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks.
If the-s option is added to the ln command, a soft link is established. If the [Link name] already exists but is not a directory, no link will be made. [Link name] can be any file name (including path), a directory, and allow it to be different from the target file system. If the [Link name] is an existing directory, the system creates one or more files with the same name as the "target" in the directory, the new file actually points to the symbolic link file of the original "target.
Example:
$ LS-IL <br/> 13058-rwx--1 longcheng 48 Aug 17 16:38 file1 <br/> 13059-rwx---2 longcheng 57 Aug 17 16:40 file2 <br/> 13059-rwx---2 longcheng 57 Aug 17 16:40 file2hard <br/> $ ln-s file1 file1soft <br/> $ ls -Il <br/> 13058-rwx---1 longcheng 48 August 15 16:38 file1 <br/> 13059-rwx---2 longcheng 57 August 15 16:40 file2 <br/> 13059-rwx--2 longcheng 57 16:40 August 5 file2hard <br/> 13061 lrwxrwxrwx 1 longcheng 5 August 5 16:58 file1soft-> file1 <br />
Soft links and hard links differ not only in terms of concept, but also in terms of implementation. Difference: A Public inode number is used for hard-link files and linked files, indicating that they are the same file, while soft-link files and linked files have different inode numbers, it indicates that they are two different files. On the file attributes, the soft link clearly writes the link file, but the hard link is not written, in essence, hard-link files are completely equal to the original files. The number of links is different, and the number of soft-link links does not increase. The file size is different, the size of the hard link file is the same as that of the original file. This is emphasized because it is equivalent, and the size of the soft link is different from that of the original file, the size of file1 is 48B, while that of file1soft is 5B, where 5 is actually the size of "file1.
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.
Disadvantages:
Because the linked file contains the path information of the original file, when the original file is moved from one directory to another, and then the linked file is accessed, the system cannot find it, however, hard links do not have this defect. You need to move them as much as you want. In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path.