首页 >> 学习 >> Linux常用命令 >> ln
ln命令

本节重要性:★★★☆☆    本节难度:★★★★★

链接分为硬链接和符号链接(软连接)。硬链接名字与原文件的名字代表的是同一个文件;而符号链接是一个特殊的文件,文件的内容是一个指向原文件路径的一个指针,所以无论原文件内容如何变化,符号链接的大小都是固定的。如果删除原文件,打开硬链接还是原文件的内容,而符号链接由于指向的原文件没有了,打开就会出错。ln命令(英文link的缩写),可以给文件建立链接。学习下面的内容前,建议先学习扩展学习里的inode一节。

命令用途

给文件或目录建立链接

命令格式

ln [option] source destination

常用选项

-s	建立符号链接(Symbolic)
-f	如果目标文件存在,则先删除目标文件

常见用法

1. 建立硬链接:

[peter@ibi98 ~]$ ls -li
总用量 20
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650773 -rw-rw-r-- 1 peter peter   62  2月 22 10:39 hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ ln hello.sh hello_hard.sh
[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello_hard.sh
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

ls的-i选项是显示文件的inode号。注意观察结果,硬链接hello_hard.sh与原文件hello.sh的inode号都是8650773,说明是同一个文件。另外需要注意的是,只能给文件建立硬链接,不能给目录建立硬链接,而且只能在同一个文件系统内建立硬链接。

2. 给文件建立符号链接:

[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello_hard.sh
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ ln -s hello.sh hello_symbolic.sh
[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello_hard.sh
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650766 lrwxrwxrwx 1 peter peter    8  2月 22 14:35 hello_symbolic.sh -> hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

注意观察结果,符号链接hello_symbolic.sh的inode号是865766,而原文件hello.sh的inode号都是8650773,说明是符号链接hello_symbolic.sh是一个新的文件,其大小与原文件也不一样,但如果打开符号链接的话,其内容与原文件是一样的,原因我们上面已经说了,打开符号链接的时候,其实打开的是其所指向的文件,也就是原文件。

3. 给目录建立符号链接:

[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello_hard.sh
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650766 lrwxrwxrwx 1 peter peter    8  2月 22 14:35 hello_symbolic.sh -> hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ ln -s /home/pub/genome/ genome
[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650768 lrwxrwxrwx 1 peter peter   17  2月 22 14:42 genome -> /home/pub/genome/
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello_hard.sh
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650766 lrwxrwxrwx 1 peter peter    8  2月 22 14:35 hello_symbolic.sh -> hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

注意,目录只有符号链接而没有硬链接。

删除硬链接或符号链接与删除文件类似:

[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650768 lrwxrwxrwx 1 peter peter   17  2月 22 14:42 genome -> /home/pub/genome/
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello_hard.sh
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650766 lrwxrwxrwx 1 peter peter    8  2月 22 14:35 hello_symbolic.sh -> hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ rm hello_hard.sh
[peter@ibi98 ~]$ rm hello_symbolic.sh
[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650768 lrwxrwxrwx 1 peter peter   17  2月 22 14:42 genome -> /home/pub/genome/
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

删除指向目录的符号链接的时候需注意,链接后不能有“/”(使用Linux命令自动补齐会在目录后自动加上“/”):

[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650768 lrwxrwxrwx 1 peter peter   17  2月 22 14:42 genome -> /home/pub/genome/
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ rm genome/
rm: 无法删除"genome/": 是一个目录
[peter@ibi98 ~]$ rm -r genome/
rm: 无法删除"genome": 符号连接的层数过多
[peter@ibi98 ~]$ rm genome
[peter@ibi98 ~]$ ls -li
总用量 24
8650767 -rw-rw-r-- 1 peter peter  365  2月 16 15:40 at_LEC1_protein.fa
8650762 drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
8650773 -rw-rw-r-- 2 peter peter   62  2月 22 10:39 hello.sh
8650763 lrwxrwxrwx 1 peter peter   17  2月 22 13:56 lily -> flower/real_lily/
8650788 drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
8650761 drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

硬链接和符号链接是Linux的重要的概念,要注意两者的区别。

<<上一节  下一节>>