首页 > Linux

Linux ln命令详解经典实例

发表于2014-09-16 20:53:33| --次阅读| 来源webkfa| 作者小猪仔

摘要:Linux ln命令详解经典实例
bsh代码
文件管理:ln
指令名称 : ln
使用权限 : 所有使用者
使用方式 : ln [options] source dist,其中 option 的格式为 :
[-bdfinsvF] [-S backup-suffix] [-V {numbered,existing,simple}]
[--help] [--version] [--]
说明 : Linux/Unix 档案系统中,有所谓的连结(link),我们可以将其视为档案的别名,而连结又可分为两种 : 硬连结(hard link)与软连结(symbolic link),硬连结的意思是一个档案可以有多个名称,而软连结的方式则是产生一个特殊的档案,该档案的内容是指向另一个档案的位置。硬连结是存在同一个档案系统中,而软连结却可以跨越不同的档案系统。
ln source dist 是产生一个连结(dist)到 source,至于使用硬连结或软链结则由参数决定。
不论是硬连结或软链结都不会将原本的档案复制一份,只会占用非常少量的磁碟空间。
参数 :
-f : 链结时先将与 dist 同档名的档案删除-d : 允许系统管理者硬链结自己的目录-i : 在删除与 dist 同档名的档案时先进行询问-n : 在进行软连结时,将 dist 视为一般的档案-s : 进行软链结(symbolic link)-v : 在连结之前显示其档名-b : 将在链结时会被覆写或删除的档案进行备份-S SUFFIX : 将备份的档案都加上 SUFFIX 的字尾-V METHOD : 指定备份的方式--help : 显示辅助说明--version : 显示版本

简单用法 :
将档案 yy 产生一个 symbolic link : zz
ln -s yy zz
将档案 yy 产生一个 hard link : zz
ln yy xx

使用实例:
实例1:给文件创建软链接
命令:
ln -s log2013.log link2013
输出:
[root@localhost test]# ll
-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log
[root@localhost test]# ln -s log2013.log link2013
[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
说明:
为log2013.log文件创建软链接link2013,如果log2013.log丢失,link2013将失效
实例2:给文件创建硬链接
命令:
ln log2013.log ln2013
输出:
[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 log2013.log ln2013
[root@localhost test]# ll
lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013
-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log
说明:
为log2013.log创建硬链接ln2013,log2013.log与ln2013的各项属性相同
实例3:接上面两实例,链接完毕后,删除和重建链接原文件
命令:
输出:
[root@localhost test]# ll
lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013
-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log
[root@localhost test]# rm -rf log2013.log 
[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 ln2013
[root@localhost test]# touch log2013.log
[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 ln2013
---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root      0 12-07 16:19 log2013.log
[root@localhost test]# vi log2013.log 
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12[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 ln2013
-rw-r--r-- 1 root root     96 12-07 16:21 log2013.log
[root@localhost test]# cat link2013 
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
[root@localhost test]# cat ln2013 
hostnamebaidu=baidu.com
hostnamesina=sina.com
hostnames=true
说明:
1.源文件被删除后,并没有影响硬链接文件;软链接文件在centos系统下不断的闪烁,提示源文件已经不存在
2.重建源文件后,软链接不在闪烁提示,说明已经链接成功,找到了链接文件系统;重建后,硬链接文件并没有受到源文件影响,硬链接文件的内容还是保留了删除前源文件的内容,说明硬链接已经失效
实例4:将文件链接为另一个目录中的相同名字
命令:
ln log2013.log test3
输出:
[root@localhost test]# ln log2013.log test3
[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 ln2013
-rw-r--r-- 2 root root     96 12-07 16:21 log2013.log
[root@localhost test]# cd test3
[root@localhost test3]# ll
-rw-r--r-- 2 root root 96 12-07 16:21 log2013.log
[root@localhost test3]# vi log2013.log 
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10[root@localhost test3]# ll
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
[root@localhost test3]# cd ..
[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 ln2013
-rw-r--r-- 2 root root     80 12-07 16:36 log2013.log
[root@localhost test]#
说明:
在test3目录中创建了log2013.log的硬链接,修改test3目录中的log2013.log文件,同时也会同步到源文件
实例5:给目录创建软链接
命令:
ln -sv /opt/soft/test/test3 /opt/soft/test/test5
输出:
[root@localhost test]# ll
drwxr-xr-x 2 root root   4096 12-07 16:36 test3
drwxr-xr-x 2 root root   4096 12-07 16:57 test5
[root@localhost test]# cd test5
[root@localhost test5]# ll
lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -> test3
[root@localhost test5]# cd test3
-bash: cd: test3: 符号连接的层数过多
[root@localhost test5]# 
[root@localhost test5]# 
[root@localhost test5]# ll
lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -> test3
[root@localhost test5]# rm -rf test3
[root@localhost test5]# ll
[root@localhost test5]# ln -sv /opt/soft/test/test3 /opt/soft/test/test5
创建指向“/opt/soft/test/test3”的符号链接“/opt/soft/test/test5/test3”
[root@localhost test5]# ll
lrwxrwxrwx 1 root root 20 12-07 16:59 test3 -> /opt/soft/test/test3
[root@localhost test5]# 
[root@localhost test5]# cd test3
[root@localhost test3]# ll
总计 4
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
[root@localhost test3]# touch log2014.log
[root@localhost test3]# ll
总计 4
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
-rw-r--r-- 1 root root  0 12-07 17:05 log2014.log
[root@localhost test3]# cd ..
[root@localhost test5]# cd ..
说明:
1.目录只能创建软链接
2.目录创建链接必须用绝对路径,相对路径创建会不成功,会提示:符号连接的层数过多 这样的错误
3.在链接目标目录中修改文件都会在源文件目录中同步变化

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1