Home  >  Article  >  Operation and Maintenance  >  What is a linux soft link?

What is a linux soft link?

青灯夜游
青灯夜游Original
2022-06-17 16:14:448419browse

In Linux, a soft link is equivalent to a shortcut in Windows, existing in the form of a path; in a soft link, the file is actually a text file that contains the location information of another file. The syntax for creating a soft link is "ln -s target source". The parameter "target" represents the target file (folder), that is, the file (folder) being pointed to, and the parameter "source" represents the soft link name of the current directory, that is, the source file. (folder).

What is a linux soft link?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Linux links are divided into two types, one is called a hard link (Hard Link), and the other is called a symbolic link (Symbolic Link), also known as a soft link.

Linux soft link

is equivalent to a shortcut in windows. Since the file created by the soft link is an independent new file, So it will occupy indoe and block

It is actually a special file. In a soft link, the file is actually a text file that contains the location information of another file.

  • Soft link exists in the form of a path. Similar to shortcuts in the Windows operating system

  • Soft links can cross file systems, but hard links cannot

  • Soft links can cross a different file system Link existing file names

  • Soft links can link directories

1. Creation syntax

ln -s target source

Explanation:

ln -s: Indicates creating a soft connection;

target: Indicates the target file (folder) [that is, the pointed file (folder)]

source: Indicates the soft link name of the current directory. [Source file (folder)]

2 Specific example

  • step 1. Create test files and folders
[root@server6 ~]# mkdir test_chk
[root@server6 ~]# touch test_chk/test.txt 
[root@server6 ~]# echo "hello spark" > test_chk/test.txt 
[root@server6 ~]# cat test_chk/test.txt 
hello spark
[root@server6 ~]# ll
总用量 84
-rw-------.  1 root root  1257 6月  16 01:17 anaconda-ks.cfg
drwxr-xr-x. 25 root root  4096 11月  1 10:28 azkabanJob
-rw-r--r--.  1 root root 67322 11月  4 10:24 azkabanJob.zip
drwxr-xr-x.  4 root root    37 7月  13 11:01 hadoop_temp
-rw-r--r--.  1 root root    54 7月   4 14:11 HelloLinux.txt
drwxr-xr-x.  2 root root    22 11月  4 10:41 test_chk
-rw-r--r--.  1 root root    67 10月  8 15:52 zookeeper.out
[root@server6 ~]# ln -s test_chk/ test_chk_ln
[root@server6 ~]# ll
总用量 84
-rw-------.  1 root root  1257 6月  16 01:17 anaconda-ks.cfg
drwxr-xr-x. 25 root root  4096 11月  1 10:28 azkabanJob
-rw-r--r--.  1 root root 67322 11月  4 10:24 azkabanJob.zip
drwxr-xr-x.  4 root root    37 7月  13 11:01 hadoop_temp
-rw-r--r--.  1 root root    54 7月   4 14:11 HelloLinux.txt
drwxr-xr-x.  2 root root    22 11月  4 10:41 test_chk
lrwxrwxrwx.  1 root root     9 11月  4 10:42 test_chk_ln -> test_chk/
-rw-r--r--.  1 root root    67 10月  8 15:52 zookeeper.out
[root@server6 ~]# cd test_chk_ln/
[root@server6 test_chk_ln]# ll
总用量 4
-rw-r--r--. 1 root root 12 11月  4 10:41 test.txt
[root@server6 test_chk_ln]# cat test.txt 
hello spark
[root@server6 test_chk_ln]# ll
总用量 4
-rw-r--r--. 1 root root 12 11月  4 10:41 test.txt
[root@server6 test_chk_ln]# cat test.txt 
hello spark

Note

#1. When creating a soft link, there is no need to create a folder.

2. Command example explanation

The executed command is: ln -s /storage/lawson/scores scor

The meaning is: change scor Point to the /storage/lawson/scores/ directory

What is a linux soft link?

Here is the current scor pointing to /storage/lawson/scores. The red color is displayed here because the directory /storage/lawson/scores does not exist. If you create this directory, you can get the blue display.

What is a linux soft link?
It should be noted that the files in all current directories cannot have the same name, because I had a folder before which was scores, so I will simply name it here. Became scor.

Deletion of soft links

rm -rf ./test_chk_ln/ will delete all the contents in the folder. But the link is not deleted;
rm -rf ./test_chk_ln only deletes the soft link and does not delete the following content.

  • Error Demonstration
[root@server6 test_chk_ln]# cd ..
[root@server6 ~]# ll
总用量 84
-rw-------.  1 root root  1257 6月  16 01:17 anaconda-ks.cfg
drwxr-xr-x. 25 root root  4096 11月  1 10:28 azkabanJob
-rw-r--r--.  1 root root 67322 11月  4 10:24 azkabanJob.zip
drwxr-xr-x.  4 root root    37 7月  13 11:01 hadoop_temp
-rw-r--r--.  1 root root    54 7月   4 14:11 HelloLinux.txt
drwxr-xr-x.  2 root root    22 11月  4 10:41 test_chk
lrwxrwxrwx.  1 root root     9 11月  4 10:42 test_chk_ln -> test_chk/
-rw-r--r--.  1 root root    67 10月  8 15:52 zookeeper.out
[root@server6 ~]# rm -rf ./test_chk_ln/
[root@server6 ~]# ll
总用量 84
-rw-------.  1 root root  1257 6月  16 01:17 anaconda-ks.cfg
drwxr-xr-x. 25 root root  4096 11月  1 10:28 azkabanJob
-rw-r--r--.  1 root root 67322 11月  4 10:24 azkabanJob.zip
drwxr-xr-x.  4 root root    37 7月  13 11:01 hadoop_temp
-rw-r--r--.  1 root root    54 7月   4 14:11 HelloLinux.txt
drwxr-xr-x.  2 root root     6 11月  4 10:42 test_chk
lrwxrwxrwx.  1 root root     9 11月  4 10:42 test_chk_ln -> test_chk/
-rw-r--r--.  1 root root    67 10月  8 15:52 zookeeper.out
[root@server6 ~]# cd test_chk
[root@server6 test_chk]# ll
总用量 0
[root@server6 test_chk]# ll
总用量 0

You can find that all the contents in the folder have been deleted. . .

  • Delete soft connections correctly
[root@server6 ~]# rm -rf ./test_chk_ln
[root@server6 ~]# ll
总用量 84
-rw-------.  1 root root  1257 6月  16 01:17 anaconda-ks.cfg
drwxr-xr-x. 25 root root  4096 11月  1 10:28 azkabanJob
-rw-r--r--.  1 root root 67322 11月  4 10:24 azkabanJob.zip
drwxr-xr-x.  4 root root    37 7月  13 11:01 hadoop_temp
-rw-r--r--.  1 root root    54 7月   4 14:11 HelloLinux.txt
drwxr-xr-x.  2 root root    22 11月  4 10:44 test_chk
-rw-r--r--.  1 root root    67 10月  8 15:52 zookeeper.out
[root@server6 ~]# cd test_chk/
[root@server6 test_chk]# ll
总用量 4
-rw-r--r--. 1 root root 12 11月  4 10:44 test.txt

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is a linux soft link?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn