Linux下文档之间的链接命令ln与link

来源:互联网 发布:python 绘制中国地图 编辑:程序博客网 时间:2024/05/29 12:13

连接的作用是在linux文件系统中把文件名和硬盘上实际的字节联系起来。比如:

echo "hello world! " > test1.txtln test1.txt test2.txt

此时用cat命令可以看到:

cat test2.txthello world!

当我们在文件系统中建立一个叫做test1的文档的时候,操作系统就把字节写入到磁盘上,并且与外部的一个文件名链接起来。如果我们修改文件名,文件本身的信息并没有改变,只是指向数据的信息改变了。


如果删除其中的任何一个文件名:

rm test1.txtcat test2.txthello world!

link 如果不带任何参数,则作用和ln相同,他们的相同作用都是创立文件名和数据的硬链接,即每一个文件名都独立链接到数据上,所以上文中删除了test2.txt 并不影响test1.txt的存在,这是跟符号链接最不相同的一点。

ln -s test1.txt test2.txt

ln还可创建符号链接,如上所示,则文件名test2.txt就是test1.txt的符号链接,test2.txt并没有指向test1.txt指向的数据,而是指向了test1.txt,所以符号链接可以理解成链接的链接。

rm test2.txtcat test1.txtcat: test2.txt: No such file or directory

如果删除test1.txt则test2.txt也无法解析。与硬链接不同的是符号链接可以链接不同分区的文档,或者创建目录的链接。

创建目录链接

ln -s /home/a2/test/ filecd filecd ..rm file

通过创立符号链接之后就可以通过链接执行目录命令了,比如cd,需要注意的是这时候如果要用rm命令,则不用家-r参数。利用符号链接可以使非常长的路径名变得便于书写。

ln命令语法详解

ln [OPTION]...  TARGET LINK_NAME
  • -b如果目标文件已经存在,就创建目标文件的备份,例如:
echo "It is a great time" >test2.txtln -b test1.txt test2.txtlstest1.txt test2.txt test2.txt~cat test2.txthello world!cat test2.txt~It is a great time
  • -f如果目标文件存在,就覆盖目标文件:
echo "It is great time">test2.txtln -f test1.txt test2.txtlstest1.txt test2.txtcat test2.txthello world!

如果目的文档本身是一个链接是,ln命令默认解引用链接:

ln -s test1.txt test2.txtln test2.txt test3.txtrm test2.txt cat test3.txthello world!
  • -i 链接时询问是否覆盖目标文件
    如过ln命令缺省目标文件,则默认在当前目录创建链接
ln -s ~/test/lstest1.txt test
0 0
原创粉丝点击