ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在 其它的目录下用ln
命令链接(link)它就可以,不必重复的占用磁盘空间。
ln [参数][源文件或目录][目标文件或目录]
Linux文件系统中,有所谓的链接(link),我们可以将其视为档案的别名,而链接又可分为两种 : 硬链接(hard link)与软链接(symbolic link),硬链接的意思是一个档案可以有多个名称,而软链接的方式则是产生一个特殊的档案,该档案的内容是指向另一个档案的位置。硬链接是存在同一个文件系统中,而软链接却可以跨越不同的文件系统。
软链接:
硬链接:
ln
命令会保持每一处链接文件的同步性,也就是说,不论你改动了哪一处,其它的文件都会发生相同的变化;ln
的链接又分软链接和硬链接两种,软链接就是ln –s
源文件 目标文件,它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间,硬链接 ln
源文件 目标文件,没有参数-s
, 它会在你选定的位置上生成一个和源文件大小相同的文件,无论是软链接还是硬链接,文件都保持同步变化。ln
指令用在链接文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则会把前面指定的所有文件或目录复制到该目录中。若同时指定多个文件或目录,且最后的目的地并非是一个已存在的目录,则会出现错误信息。必要参数:
-b
删除,覆盖以前建立的链接-d
允许超级用户制作目录的硬链接-f
强制执行-i
交互模式,文件存在则提示用户是否覆盖-n
把符号链接视为一般目录-s
软链接(符号链接)-v
显示详细的处理过程-S
“-S<字尾备份字符串> ”或 “--suffix=
<字尾备份字符串>”-V
“-V<备份方式>”或“--version-control=
<备份方式>”--help
显示帮助信息--version
显示版本信息命令:
ln -s newlog.log link2log
输出:
[zyiz@localhost ~]$ ll total 300 drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-r--r--. 1 root root 293561 Feb 19 22:31 lsof.txt -rw-rw-r-- 1 zyiz zyiz 572 Feb 23 19:58 newlog.log -rw-------. 1 zyiz zyiz 0 Feb 19 20:54 nohup.out drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 -rw-rw-r--. 1 zyiz zyiz 19 Feb 15 21:08 text.txt [zyiz@localhost ~]$ ln -s newlog.log link2log [zyiz@localhost ~]$ ll total 300 lrwxrwxrwx 1 zyiz zyiz 10 Feb 23 19:58 link2log -> newlog.log drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-r--r--. 1 root root 293561 Feb 19 22:31 lsof.txt -rw-rw-r-- 1 zyiz zyiz 572 Feb 23 19:58 newlog.log -rw-------. 1 zyiz zyiz 0 Feb 19 20:54 nohup.out drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 -rw-rw-r--. 1 zyiz zyiz 19 Feb 15 21:08 text.txt [zyiz@localhost ~]$
说明:为 newlog.log
文件创建软链接link2log
,如果newlog.log
丢失,link2log
将失效。
命令:
ln run.py ln2run.py
输出:
[zyiz@localhost ~]$ ll total 4 drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 [zyiz@localhost ~]$ ln run.py ln2run.py [zyiz@localhost ~]$ ll total 8 -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 ln2run.py drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 [zyiz@localhost ~]$
说明: 为run.py
创建硬链接ln2run.py
,run.py
与ln2run.py
的各项属性相同
命令执行和输出:
[zyiz@localhost ~]$ ll total 12 lrwxrwxrwx 1 zyiz zyiz 10 Feb 23 20:04 link2log -> newlog.log -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 ln2run.py drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-rw-r-- 1 zyiz zyiz 330 Feb 23 20:05 newlog.log drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 [zyiz@localhost ~]$ rm -rf newlog.log [zyiz@localhost ~]$ ll total 8 lrwxrwxrwx 1 zyiz zyiz 10 Feb 23 20:04 link2log -> newlog.log -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 ln2run.py drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 [zyiz@localhost ~]$ du /home/zyiz/project > newlog.log [zyiz@localhost ~]$ cat newlog.log /home/zyiz/project/lib /home/zyiz/project/bin /home/zyiz/project/doc/info /home/zyiz/project/doc/product /home/zyiz/project/doc /home/zyiz/project/logs/info /home/zyiz/project/logs/product /home/zyiz/project/logs /home/zyiz/project/service/deploy/info /home/zyiz/project/service/deploy/product /home/zyiz/project/service/deploy /home/zyiz/project/service /home/zyiz/project [zyiz@localhost ~]$ ll total 12 lrwxrwxrwx 1 zyiz zyiz 10 Feb 23 20:04 link2log -> newlog.log -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 ln2run.py drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-rw-r-- 1 zyiz zyiz 431 Feb 23 20:06 newlog.log drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 2 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 177 Feb 16 21:28 test4 [zyiz@localhost ~]$
说明:
命令:
ln newlog.log test4
输出:
[zyiz@localhost ~]$ ll total 8 drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-rw-r-- 1 zyiz zyiz 559 Feb 23 20:13 newlog.log drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 6 Feb 23 20:11 test4 [zyiz@localhost ~]$ cat newlog.log /home/zyiz/test4 /home/zyiz/project/lib /home/zyiz/project/bin /home/zyiz/project/doc/info /home/zyiz/project/doc/product /home/zyiz/project/doc /home/zyiz/project/logs/info /home/zyiz/project/logs/product /home/zyiz/project/logs /home/zyiz/project/service/deploy/info /home/zyiz/project/service/deploy/product /home/zyiz/project/service/deploy /home/zyiz/project/service /home/zyiz/project /home/zyiz/logs/test3 /home/zyiz/logs /home/zyiz/scripts /home/zyiz/.ssh /home/zyiz/ [zyiz@localhost ~]$ [zyiz@localhost ~]$ [zyiz@localhost ~]$ ll total 8 drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-rw-r-- 1 zyiz zyiz 559 Feb 23 20:13 newlog.log drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 6 Feb 23 20:11 test4 [zyiz@localhost ~]$ ln newlog.log test4 [zyiz@localhost ~]$ ll test4/ total 4 -rw-rw-r-- 2 zyiz zyiz 559 Feb 23 20:13 newlog.log [zyiz@localhost ~]$ cat test4/newlog.log /home/zyiz/test4 /home/zyiz/project/lib /home/zyiz/project/bin /home/zyiz/project/doc/info /home/zyiz/project/doc/product /home/zyiz/project/doc /home/zyiz/project/logs/info /home/zyiz/project/logs/product /home/zyiz/project/logs /home/zyiz/project/service/deploy/info /home/zyiz/project/service/deploy/product /home/zyiz/project/service/deploy /home/zyiz/project/service /home/zyiz/project /home/zyiz/logs/test3 /home/zyiz/logs /home/zyiz/scripts /home/zyiz/.ssh /home/zyiz/ [zyiz@localhost ~]$
说明:在test4
目录中创建了newlog.log
的硬链接,修改test4
目录中的newlog.log
文件,同时也会同步到源文件。
命令:
ln -sv /usr/local/src /home/zyiz/dir2src
输出:
[zyiz@localhost ~]$ ll total 8 drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-rw-r-- 2 zyiz zyiz 559 Feb 23 20:13 newlog.log drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 24 Feb 23 20:13 test4 [zyiz@localhost ~]$ ln -sv /usr/local/src /home/zyiz/dir2src ‘/home/zyiz/dir2src’ -> ‘/usr/local/src’ [zyiz@localhost ~]$ ll total 8 lrwxrwxrwx 1 zyiz zyiz 14 Feb 23 20:17 dir2src -> /usr/local/src drwxrwxr-x. 3 zyiz zyiz 35 Feb 16 20:54 logs -rw-rw-r-- 2 zyiz zyiz 559 Feb 23 20:13 newlog.log drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project -rw-rw-r--. 1 zyiz zyiz 12 Feb 23 19:57 run.py drwxrwxr-x. 2 root zyiz 84 Feb 18 09:34 scripts drwxrwxr-x. 2 zyiz zyiz 24 Feb 23 20:13 test4 [zyiz@localhost ~]$ cd dir2src/ [zyiz@localhost dir2src]$ ll total 163804 -rw-r--r--. 1 root root 167733100 Feb 15 02:46 jdk-8u121-linux-x64.rpm [zyiz@localhost dir2src]$ ll /usr/local/src/ total 163804 -rw-r--r--. 1 root root 167733100 Feb 15 02:46 jdk-8u121-linux-x64.rpm [zyiz@localhost dir2src]$
说明:
软链接就相当于windows的的快捷方式,使用场景:
硬链接说白了是一个指针,指向文件索引节点,系统并不为它重新分配inode.通过使用硬链接可达到备份数据(实际是备份节点)的效果。