在Linux系统中,命令ln(英文全称:link files)用于将某一个文件在另外一个位置建立一个同步链接(功能就好比Windows系统中我们建立的快捷方式一样)。
【扩展】
所谓的链接(link)我们可以将其视为文件的别名,而链接又可以分为符号链接/软链接(symbolic link)与硬链接,软链接的意思是产生一个特殊的文件,这个文件的内容是指向另一个文件的位置;硬链接的意思是一个文件可以有多个名称。另外,软连接可以存在于不同的文件系统中,而软连接只能存在yu同一个文件系统中,无论是软连接还是硬链接都不会将源文件复制一份,因此链接(link)占用的磁盘空间非常小。
1.软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
2.软链接可以 跨文件系统 ,硬链接不可以
3.软链接可以对一个不存在的文件名进行链接
4.软链接可以对目录进行链接
1.硬链接,以文件副本的形式存在。但不占用实际空间。
2.不允许给目录创建硬链接
3.硬链接只有在同一个文件系统中才能创建
ln [选项] [源文件或目标文件] [源文件或目标文件]
-b 删除,覆盖以前建立的链接
-d 允许超级用户制作目录的硬链接
-f 强制执行
-i 交互模式,文件存在则提示用户是否覆盖
-n 把符号链接视为一般目录
-s 软链接(符号链接)
-v 显示详细的处理过程
案例:
给test.log文件创建软连接 testlink, 若test.log丢失,则软连接testlink则失效
[root@centos test]# ll
total 4
-rw-r--r--. 1 root root 62 Oct 12 14:26 test.log
[root@centos test]# ln -s test.log testlink
[root@centos test]# ll
total 4
lrwxrwxrwx. 1 root root 8 Oct 12 14:27 testlink -> test.log
-rw-r--r--. 1 root root 62 Oct 12 14:26 test.log
[root@centos test]#
[root@centos test]# cat test.log
111111111111111111111
22222222222222222222
333333333333333333
[root@centos test]# cat testlink
111111111111111111111
22222222222222222222
333333333333333333
[root@centos test]#
给test.log文件创建硬链接testhard,test.log与testhard的各项属性相同
[root@localhost test]# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
[root@localhost test]# ln test.log testhard
[root@localhost test]# ll
-rw-r--r-- 2 root bin 61 11-13 06:03 testhard
-rw-r--r-- 2 root bin 61 11-13 06:03 test.log