硬链接和软链接

来源:互联网 发布:韦德去骑士数据 编辑:程序博客网 时间:2024/05/06 16:25


1)硬链接

   必须在一个分区,两个文件互为源,命硬,删掉一个还有一个。多个文件共享一个inode。是为了节省inode资源。这多个文件的内容是同步的。

[root@foundation4 ~]# cd /home/kiosk/Desktop/

[root@foundation4 Desktop]# touch file

[root@foundation4 Desktop]# ln file newfile

[root@foundation4 Desktop]# ls

file  newfile  rht-desktop.desktop  rht-server.desktop

[root@foundation4 Desktop]# ls -li file newfile

137484361 -rw-r--r--. 2 root root 0 Oct 26 09:32 file

137484361 -rw-r--r--. 2 root root 0 Oct 26 09:32 newfile

[root@foundation4 Desktop]# echo hahaha > file

[root@foundation4 Desktop]# cat file newfile

hahaha

hahaha

[root@foundation4 Desktop]# rm -f file

[root@foundation4 Desktop]# ls

newfile  rht-desktop.desktop  rht-server.desktop

[root@foundation4 Desktop]# ll newfile 

-rw-r--r--. 1 root root 7 Oct 26 09:33 newfile

[root@foundation4 Desktop]# ll -i newfile 

137484361 -rw-r--r--. 1 root root 7 Oct 26 09:33 newfile

[root@foundation4 Desktop]#  

硬链接有两个限制:

1.不允许个目录创建硬链接

2.只有在同一文件系统中的文件才能创建链接

2)软链接

    可以不在一个分区,只有一个最本质的源文件。删掉源另一个也就失效了。两个文件的inode号是不同的。源文件有100M 链接文件可能只有几十K。只要源文件消失,这个文件就无法访问,链接文件只是访问源文件的一个跳板。

[root@foundation4 Desktop]# touch file

[root@foundation4 Desktop]# ln -s file softfile

[root@foundation4 Desktop]# ls -li

total 8

137484360 -rw-r--r--. 1 root  root    0 Oct 26 09:45 file

137473834 -rwxrwxr-x. 1 kiosk kiosk 294 Oct 25 09:10 rht-desktop.desktop

137473837 -rwxrwxr-x. 1 kiosk kiosk 289 Oct 25 09:10 rht-server.desktop

137484369 lrwxrwxrwx. 1 root  root    4 Oct 26 09:46 softfile -> file

[root@foundation4 Desktop]# echo kkkkkkkkk > file

[root@foundation4 Desktop]# rm -f file

[root@foundation4 Desktop]# cat file softfile

kkkkkkkkk

kkkkkkkkk

[root@foundation4 Desktop]# rm -rf file

[root@foundation4 Desktop]# ls -li

total 8

137473834 -rwxrwxr-x. 1 kiosk kiosk 294 Oct 25 09:10 rht-desktop.desktop

137473837 -rwxrwxr-x. 1 kiosk kiosk 289 Oct 25 09:10 rht-server.desktop

137484369 lrwxrwxrwx. 1 root  root    4 Oct 26 09:46 softfile -> file

0 0