1.功能:将一个文件或者文件夹链接到另外一个文件或者文件夹上。链接分为硬链接和软链接,硬链接可以看做是一个文件具有多个访问的入口,软链接可以看成是快捷方式。

2.用法:ln [选项] 源文件或目录 目标文件或目录

3.参数:

-P, --physical 硬链接

-s, --symbolic 软链接

4.例子

例1:分别创建硬链接和软链接

[root@mycentos test]# echo "Today is Tuesday">>test_1.txt

[root@mycentos test]# cat test_1.txt 

Today is Tuesday

[root@mycentos test]# ls -lh

总用量 4.0K

-rw-r--r-- 1 root root 17 5月  17 17:36 test_1.txt

[root@mycentos test]# ln test_1.txt hard_test.txt

[root@mycentos test]# ls -lh

总用量 8.0K

-rw-r--r-- 2 root root 17 5月  17 17:36 hard_test.txt

-rw-r--r-- 2 root root 17 5月  17 17:36 test_1.txt

[root@mycentos test]# ln -s test_1.txt soft_test.txt

[root@mycentos test]# ls -lh

总用量 8.0K

-rw-r--r-- 2 root root 17 5月  17 17:36 hard_test.txt

lrwxrwxrwx 1 root root 10 5月  17 17:38 soft_test.txt -> test_1.txt

-rw-r--r-- 2 root root 17 5月  17 17:36 test_1.txt

例2:删除 test_1.txt 后,硬链接的文件可以正常访问,并且内容和源文件内容一样,说明文件可以正常访问,但是软链接的文件颜色改变。

Linux常用命令汇总--ln_ln