The ln -s command of Linux command (soft link)

ln command

The Linux ln (full English spelling: link files) command is a very important command. Its function is to establish a synchronous link for a certain file in another location.
When we need to use the same file in different directories, we don't need to put a file that must be the same in every required directory, we just need to put the file in a fixed directory, and then in other You can use the ln command to link (link) it in the directory, and you don’t have to take up disk space repeatedly.
There are two ways to link files in the Linux system:

  • soft link
  • hard link

If the file is deleted, the soft link file loses its point and becomes unavailable
If the file is deleted, the hard link file is not affected because it directly points to the content

create hard link

ln [源文件/目录] [目标软链接]

Create a soft connection

ln -s [源文件/目录] [目标软链接]
[root@asd test]# mkdir data-directory-test1
[root@asd test]# touch data-file-test2
[root@asd test]# ln -s data-directory-test1/ test1
[root@asd test]# ln -s data-file-test2 test2
 
[root@node02 test]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 10 16:24 data-directory-test1
-rw-r--r-- 1 root root    0 Nov 10 16:26 data-file-test2
lrwxrwxrwx 1 root root   21 Nov 10 16:27 test1 -> data-directory-test1/
lrwxrwxrwx 1 root root   15 Nov 10 16:28 test2 -> data-file-test2

delete soft link

Deleting a soft link is the same as deleting a file/directory, using the rm command. However, special attention should be paid to the fact that if you just delete the soft link and do not want to delete the real file data of the soft link directory by mistake, you must not have "/" at the end of the path in the rm directory. If "/" is included, all files under the linked path will be deleted.

[root@node02 test]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 10 16:46 data-directory-test1
lrwxrwxrwx 1 root root   20 Nov 10 16:46 test1-1 -> data-directory-test1
lrwxrwxrwx 1 root root   20 Nov 10 16:46 test1-2 -> data-directory-test1
[root@node02 test]# ll data-directory-test1/
total 0
-rw-r--r-- 1 root root 0 Nov 10 16:47 file1
-rw-r--r-- 1 root root 0 Nov 10 16:47 file2
 
 
#末尾不带"/"删除,删除软链接,不删除目录下数据
[root@node02 test]# rm -rf test1-1
[root@node02 test]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 10 16:47 data-directory-test1
lrwxrwxrwx 1 root root   20 Nov 10 16:46 test1-2 -> data-directory-test1
[root@node02 test]# ll data-directory-test1/
total 0
-rw-r--r-- 1 root root 0 Nov 10 16:47 file1
-rw-r--r-- 1 root root 0 Nov 10 16:47 file2
 
 
#末尾带"/"删除,删除目录下数据,但不删除软链接
[root@node02 test]# rm -rf test1-2/
[root@node02 test]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 10 16:48 data-directory-test1
lrwxrwxrwx 1 root root   20 Nov 10 16:46 test1-2 -> data-directory-test1
[root@node02 test]# ll data-directory-test1/
total 0

Modify soft link

ln -snf [新源文件/目录] [目标软链接]
[root@node02 test]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 10 16:48 data-directory-test1
drwxr-xr-x 2 root root 4096 Nov 10 16:51 data-directory-test2
lrwxrwxrwx 1 root root   20 Nov 10 16:51 test-link -> data-directory-test1
 
[root@node02 test]# ln -snf data-directory-test2 test-link
[root@node02 test]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 10 16:52 data-directory-test1
drwxr-xr-x 2 root root 4096 Nov 10 16:52 data-directory-test2
lrwxrwxrwx 1 root root   20 Nov 10 16:52 test-link -> data-directory-test2

Guess you like

Origin blog.csdn.net/weixin_42648692/article/details/129998333