硬链接:
由于linux下的文件是通过索引节点(Inode)来识别文件,系统并不为硬连接重新分配inode。每添加一个硬链接,文件的链接数就加1,但是硬连接本身还是有的原文件的inode值。如下:
[root@localhost tmp]# vi test
[root@localhost tmp]# ln test test-hd
[root@localhost tmp]# ls -il test*
1041459 -rw-r--r-- 2 root root 0 2012-02-13 20:46 test
1041459 -rw-r--r-- 2 root root 0 2012-02-13 20:46 test-hd
可以看到inode值都是1041459,连接数为2。
限制:1)不可以在不同文件系统的文件间建立链接;2)只有超级用户才可以为目录创建硬链接。
符号链接(软链接):
符号链接没有任何文件系统的限制,任何用户可以创建指向目录的符号链接,其实质上就是建立一个独立文件,这个文件会让数据的读取指向它连接的文件的内容,相当于windows系统中的快捷方式。而符号连接所占用的空间大小就是所连接的文件名长度大小。如下:
[root@localhost tmp]# ln -s test test-so
[root@localhost tmp]# ls -il test*
1041459 -rw-r--r-- 2 root root 0 2012-02-13 20:46 test
1041459 -rw-r--r-- 2 root root 0 2012-02-13 20:46 test-hd
1041458 lrwxrwxrwx 1 root root 4 2012-02-13 20:52 test-so -> test