This is a very important command in Linux, please be familiar with it. Its function is to create a link to a file in another location, the most common parameter of this command is-s,
The specific usage is: Ln-s source file Destination file.
when we need to use the same file in different directories, we do not need to put a file in each required directory, we just put the file in a fixed directory, and then in the other directory with the ln command link it can, do not have to repeatedly occupy disk space. Example: Ln-s/bin/less/usr/local/bin/less
- S is the meaning of the Code name (symbolic).
here are two points to note: first, the LN command will maintain the synchronization of each linked file, that is, no matter where you change, the other files will change the same; second, ln links and soft links and hard links two, soft link is ln-s * *, It will only generate a mirror image of the file in the location you selected, not disk space, hard link ln * *, no parameter-s, it will generate a file in your chosen location with the same size as the source file, whether it is a soft link or a hard link, the file will remain in sync.
whether a hard link or a soft link will not copy the original file, it will only occupy a very small amount of disk space.
-F: Delete the file with the Dist file name at the end of the link
-D: Allow system managers to hard-link their own directories
-I: Ask before deleting the file with the Dist file name
- N: When making a soft link, treat the dist as a generic file
-S: Soft junction (symbolic Link)
- V: Displays the file name before the link
-B: Files that are overwritten or deleted at the time of the link are backed up
-S SUFFIX: Add the backed up files with SUFFIX tails
-V Method: Specify how the backup should be
--help: Show Auxiliary Instructions
--version: Display version
"Hard Connect"
A hard connection is a connection that is made through an index node. In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode index). In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. The reason for this is as above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself and other connections, and the connection to the file's data block and directory will be released only if the last connection is deleted. That is, the condition that the file is actually deleted is that all the hard connection files associated with it are deleted.
"Soft Connect"
Another connection is called a symbolic connection (symbolic link), also known as a soft connection. A soft-link file has a shortcut similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains location information for another file.
2. Deepen understanding through experimentation
[Email protected]]$ vi test.log #创建一个测试文件f1
[Email protected]]$ ln test.log test1.log #创建f1的一个硬连接文件test1. Log
[Email protected]]$ ln-s test.log test2.log #创建f1的一个符号连接文件test2. Log
[[Email protected]]$ ls-li #-I parameter displays inode node information for a file
The difference between the two types of links:
There are two restrictions on hard-link files
1), do not allow to create hard links to the directory;
2), links can only be created between files in the same file system, and only Superuser has the ability to establish hard-link permissions.
When you read and write to a hard-link file, the result is the same as a soft link. But if we delete the source file of the hard-link file, the hard-link file still exists, and the content of the wish is preserved.
At this point, the system "forgot" it was once a hard link file. And treat him like a regular file.
So we can understand this: a hard connection is a connection made through an index node, which allows a file to have more than one valid pathname, which can be used to delete the error.
The reason for this is because the corresponding file's index node has more than one connection. Deleting only one connection does not affect the index node itself and other
Connection will be released only if the last connection is deleted and the file's data block and directory are connected. The file will not be really deleted.
Note: Files that exist on a disk partition are assigned a number, called the Index node number (Inode index, the I node), regardless of the type.
Soft links do not have a hard link above the two limitations, so now more widely used, it has greater flexibility, even can be across different machines, different networks to link files. However, the disadvantage of soft link is: Because the link file contains the path information of the original file, so when the original file from one directory to another directory, and then access the linked file, the system will not be found, and the hard link does not have this flaw, you want to move how to move And it wants the system to allocate additional space for creating a new index node and saving the path to the original file.
Linux Create connection command ln-s Soft Connect