Detailed explanation of ln commands in linux commands (creating soft links and hard links) and ln commands
Ln is another important command in linux. Its function is to create a synchronous link for a file in another location, which can be soft link or hard link. Soft links are similar to shortcuts for windows. The following are usage methods and examples.
Ln is another important command in linux. Its function is to create a synchronization link for a file in another location. when we need to use the same file in different directories, we do not need to put a file that must be the same under each required directory. We only need to put it in a fixed directory, put the file, and then use the ln command link in other directories to link it, without occupying disk space repeatedly.
1. Command Format:
Ln [parameter] [source file or directory] [target file or directory]
2. command functions:
In a Linux File System, there is a so-called link. we can regard it as the alias of the file, and there are two types of links: hard link) with symbolic link, a hard link means that a file can have multiple names, while a soft link generates a special file, the content of this file points to another file. Hard links exist in the same file system, but soft links can span different file systems.
Soft link:
1. Soft links exist as paths. Similar to shortcuts in Windows
2. Soft links can be cross-file systems, but hard links cannot.
3. Soft links can be used to link a non-existent file name
4. Soft links can be used to link Directories
Hard link:
1. Hard links exist in the form of file copies. But it does not occupy the actual space.
2. Hard links cannot be created for directories.
3. Hard links can only be created in the same file system
Note the following two points:
First, the ln command will keep the synchronization of each link file. That is to say, other files will change the same no matter which one you have changed;
Second, there are two types of ln links: Soft link and hard link. The soft link is the target file of the ln-s source file. It will only generate an image of the file at the position you selected, it does not occupy disk space. It is hard linked to the target file of the ln source file without the parameter-s. It will generate a file of the same size as the source file at the position you selected, both soft links and hard links are synchronized.
Ln commands are used to link files or directories. For example, if you specify more than two files or directories at the same time, and the final destination is an existing Directory, all the previously specified files or directories will be copied to this directory. If multiple files or directories are specified at the same time and the destination is not an existing Directory, an error message is displayed.
3. command parameters:
Required parameters:
-B: delete the file, overwrite the previously created link.
-D allows the superuser to create hard links to directories.
-F Force Execution
-I interaction mode. If a file exists, the system prompts the user to overwrite the file.
-N indicates the symbolic link as a general directory.
-S soft link (symbolic link)
-V: detailed handling process
Select parameters:
-S "-S <suffix backup string>" or "-- suffix = <suffix backup string>"
-V "-V <backup mode>" or "-- version-control = <backup mode>"
-- Help: displays help information.
-- Version: displays version information.
4. Example:
Instance 1: Create a soft link to the file
Command: ln-s log2013.log link2013
Output:
The Code is as follows:
[Root @ localhost test] # ll
-Rw-r -- 1 root bin 61 11-13 06:03 log2013.log
[Root @ localhost test] # ln-s log2013.log link2013
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 log2013.log
Note: Create a soft link link2013 for the log2013.log file. If log2013.log is lost, link2013 will become invalid.
Instance 2: Create a hard link to the file
Command: ln log2013.log ln2013
Output:
The Code is as follows:
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 log2013.log
[Root @ localhost test] # ln log2013.log ln2013
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 2 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root bin 61 11-13 06:03 log2013.log
Note: Create a hard link ln2013 for log2013.log. log2013.log has the same attributes as ln2013.
Instance 3: connect the two instances. After the link is complete, delete and recreate the original file.
Command: ll
Output:
The Code is as follows:
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 2 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root bin 61 11-13 06:03 log2013.log
[Root @ localhost test] # rm-rf log2013.log
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
[Root @ localhost test] # touch log2013.log
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
--- Xrw-r -- 1 root bin 302108 11-13 log2012.log
-Rw-r -- 1 root 0 12-07 16:19 log2013.log
[Root @ localhost test] # vi log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12 [root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
-Rw-r -- 1 root 96 12-07 16:21 log2013.log
[Root @ localhost test] # cat link2013
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
[Root @ localhost test] # cat ln2013
Hostnamebaidu = baidu.com
Hostnamesina = sina.com
Hostnames = true
Note:
1. After the source file is deleted, the hard link file is not affected. The soft link file is continuously flashing in the centos system, prompting that the source file does not exist.
2. after the source file is re-built, the soft link does not flash, indicating that the link is successful and the linked file system is found. After reconstruction, the hard link file is not affected by the source file, the content of the source file before deletion is retained, indicating that the hard link is invalid.
Example 4: link the file to the same name in another directory
Command: ln log2013.log test3
Output:
The Code is as follows:
[Root @ localhost test] # ln log2013.log test3
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root 96 12-07 16:21 log2013.log
[Root @ localhost test] # cd test3
[Root @ localhost test3] # ll
-Rw-r -- 2 root 96 12-07 16:21 log2013.log
[Root @ localhost test3] # vi log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10 [root @ localhost test3] # ll
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
[Root @ localhost test3] # cd ..
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
[Root @ localhost test] #
Note: The hard link of log2013.log is created in the test3 directory, and the log2013.log file in the test3 directory is modified and synchronized to the source file.
Instance 5: Create a soft link to the directory
Command: ln-sv/opt/soft/test/test3/opt/soft/test/test5
Output:
The Code is as follows:
[Root @ localhost test] # ll
Drwxr-xr-x 2 root 4096 12-07 16:36 test3
Drwxr-xr-x 2 root 4096 12-07 16:57 test5
[Root @ localhost test] # cd test5
[Root @ localhost test5] # ll
Lrwxrwxrwx 1 root 5 12-07 16: 57 test3-> test3
[Root @ localhost test5] # cd test3
-Bash: cd: test3: too many symbolic connections
[Root @ localhost test5] #
[Root @ localhost test5] #
[Root @ localhost test5] # ll
Lrwxrwxrwx 1 root 5 12-07 16: 57 test3-> test3
[Root @ localhost test5] # rm-rf test3
[Root @ localhost test5] # ll
[Root @ localhost test5] # ln-sv/opt/soft/test/test3/opt/soft/test/test5
Create the Symbolic Link "/opt/soft/test/test3" to "/opt/soft/test/test5/test3"
[Root @ localhost test5] # ll
Lrwxrwxrwx 1 root 20 12-07 :59 test3->/opt/soft/test/test3
[Root @ localhost test5] #
[Root @ localhost test5] # cd test3
[Root @ localhost test3] # ll
Total 4
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
[Root @ localhost test3] # touch log2014.log
[Root @ localhost test3] # ll
Total 4
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
-Rw-r -- 1 root 0 12-07 17:05 log2014.log
[Root @ localhost test3] # cd ..
[Root @ localhost test5] # cd ..
Note:
1. The directory can only create soft links
2. The Directory Creation link must use an absolute path. The relative path cannot be created successfully. The following error occurs: too many layers of symbolic connections.
3. Files modified in the target directory of the link will be synchronized and changed in the source directory.