Linux Shell Basics: ln Command

ln command usage:

ln [options] target link_name
$ echo "this is a log file" > log.txt
# 创建软链接,相当于一个快捷方式
$ ln -s log.txt soft_log
# 创建一个硬链接
$ ln log.txt hard_log

# 创建链接后,对链接的读写,都会反应在源文件上
$ ll
-rw-rw-r-- 2 dev dev   19 227 13:15 hard_log
-rw-rw-r-- 2 dev dev   19 227 13:15 log.txt
lrwxrwxrwx 1 dev dev    7 227 13:16 soft_log -> log.txt # 彩色命令行终端上,soft_log显示为淡蓝色
$ cat hard_log 
this is a log file
$ cat soft_log 
this is a log file
$ echo 'hard link' >> hard_log 
$ cat log.txt 
this is a log file
hard link
$ echo 'soft link' >> soft_log 
$ cat log.txt 
this is a log file
hard link
soft link
$ cat hard_log 
this is a log file
hard link
soft link

# 删除源文件,硬链接仍可用,软链接不可用
$ rm log.txt
$ ls -l
-rw-rw-r-- 2 dev dev 19 227 13:15 hard_log
lrwxrwxrwx 1 dev dev  7 227 13:16 soft_log -> log.txt # 命令行终端上,显示为红色
$ cat hard_log 
this is a log file
$ cat soft_log 
cat: soft_log: No such file or directory

This command is relatively simple and is frequently used in development. And the most commonly used is the usage of creating soft links.
Just give a few examples:
1) On Ubuntu, multiple versions of the same software can be installed, the name with the version number is installed, and the name without the version number is created through a soft link. Both the system and users use the version without version number (link) by default, which can achieve the effect of compatibility of various versions.
This method is applied to executable files, dynamic libraries, etc.
2) In the source code of the project in charge, the scattered header file directories can be concentrated into an include directory through links, and all the header files used can be searched by referencing this include directory.

todo

Guess you like

Origin blog.csdn.net/yinminsumeng/article/details/129240043