Linux战地日记——软链接和硬链接

网上查阅一些相关资料,测试了一些软链接与硬链接实例,对软、硬链接有了具体的了解。

通过ln命令建立软、硬链接

建立软链接$  ln  -s  原文件  软链接文件

[bestcoder@localhost ~]$ ln -s ./test1/f1.c ./test1/f1.soft

建立硬链接$  ln  原文件  硬链接文件

[bestcoder@localhost ~]$ ln ./test1/f1.c ./test1/f1.hard

查看软链接详细信息:

[bestcoder@localhost ~]$ ls -l ./test1/f1.c ./test1/f1.soft
-rw-rw-r--. 2 bestcoder bestcoder 57 9月  11 20:05 ./test1/f1.c
lrwxrwxrwx. 1 bestcoder bestcoder 12 9月  12 20:40 ./test1/f1.soft -> ./test1/f1.c

查看硬链接详细信息:
[bestcoder@localhost ~]$ ls -l ./test1/f1.c ./test1/f1.hard
-rw-rw-r--. 2 bestcoder bestcoder 57 9月  11 20:05 ./test1/f1.c
-rw-rw-r--. 2 bestcoder bestcoder 57 9月  11 20:05 ./test1/f1.hard


删除原文件后:

[bestcoder@localhost ~]$ rm ./test1/f1.c
[bestcoder@localhost ~]$ cat ./test1/f1.soft  //软链接失效
cat: ./test1/f1.soft: 没有那个文件或目录
[bestcoder@localhost ~]$ cat ./test1/f1.hard  //硬链接不受影响
#include<stdio.h>
int main(){
printf ("hello world!");
}


总结:软链接作用相当于windows中的快捷方式,具有与原文件不同的iNode,显示信息中有一个明显的 ->指向符号。如果删除了原来的文件,软链接文件也会失效。

硬链接作用相当于cp命令 + 同步更新,具有与原文件相同的iNode,文件信息几乎完全相同。当原文件信息发生变化时,硬链接文件也会变化。如果删除了原来的文件,硬链接文件不受影响。

注意,软链接可跨文件系统,而硬链接不可跨文件系统。


你可能感兴趣的:(操作系统,linux战地日记)