1、创建硬链接
函数原型
int link(const charoldpath,const charnewpath);
- oldpath:原文件
- newpath:硬链接文件
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
link("helllo", "hello.hard");
return 0;
}
输出:
2、软连接
int symlink(const char*target,const char *linkpath)
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
symlink("helllo", "hello.soft1");
return 0;
}
输出:
3、删除符号链接或者硬链接计数
int unlink(const char*pathname);
注意:
- 可以删掉软连接也可以删掉硬链接,文件也可以
- 有其他进程使用该文件,unlink可以先不删,继续执行操作,在进程退出后文件删掉
4、读取符号链接本身内容,得到连接指向的文件名
函数原型:ssize_t readlink(const char*path,char *buf,size_t bufsiz);
- pathname :链接名
- buf:缓冲区
- bufsiz:缓冲区大小
成功:返回buf填充大小,失败返回-1
注意:读取软连接,不能读取硬链接
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
char buf[32]={0};
readlink("hello.soft1",buf,sizeof(buf));
printf("buf is %s\n",buf);
return 0;
}
输出: