Hard Link && Symbol Link
Outline:
- link
- symlink
硬链接( link )
对应的shell命令:ln
对应系统调用link
详解
硬链接只是对同一个inode创建了新的引用
1
2
3
4ln far far2
ls -i
2536724 far 2536724 far2 //inode number一样事实上,文件名都只是对
inode的链接创建文件时,实际上是先创建
inode,然后将人类可读的名称链接到该文件,并将这个键值对存入目录不能创建目录的硬链接,因为会在目录树中成环
inode number在不同文件系统中不唯一,因此硬链接不能跨文件系统硬链接会增加文件的引用计数,也就是
ls -l里看到的那个
code
Create a new link to (make a new name for) a file:
1 |
|
- Return: 0 if success; -1 if failure
Delete a name and possibly the file it refers to:
1 | #include <unistd.h> |
- Return: 0 if success; -1 if failure
软链接( symlink )
也称为符号链接
对应shell命令:ln -s
1 | ln -s far far3 |
对应系统调用symlink
详解
符号链接是一个不同类型的文件, 它的内容是被链接文件的文件名
ls显示,类型为l
OS将截获对符号链接文件的访问,,依据符号链接中的文件名去读真正的目标文件
优点: 可链接目录, 可跨文件系统链接(因为只存储了目标路径)
缺点:搜索文件路径开销大,需要额外的空间查找存储路径
1 | ls -al |
code
Create a symbolic link (named newpath which containsthesting”oldpath”):
1 |
|
- Return: 0 if success; -1 if failure
Read value of a symbolic link:
1 |
|
- Return: the count of characters placed in the buffer if success; -1 if failure