【Linux-ln拓展】可用于同时创建多个硬链接同时备份多个文件的程序
来源:互联网 发布:java web文件管理系统 编辑:程序博客网 时间:2024/06/05 14:11
最经学习了硬链接命令ln,但发现无法同时创建多个硬链接。同时也为了以后备份资料,以防止误删产生的严重后果。做了一个小程序lnd;
format:lnd sorceDirectoryPath destinationDirecotyPath
功能:为sorceDirectoryPath 中的各个文件创建一个硬链接到destinationDirecotyPath中,若destinationDirecotyPath不存在,那么将递归创建目录
缺点:若sorceDirectoryPath 中也含有其他目录,则不进行递归操作
若想在系统任意地方使用该命令,那么将lnd的可执行程序放入 /bin 中, lnd在当前目录下:sudo mv lnd /bin
附上源码:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
/*
format: lnd srcDir desDir
func: make a backups to the file of "srcDir" directory in the objDir.
if objDir is not exited, the func will create it.
*/
int main(const int argc,const char* argv[])
{
if (argc < 3)
{
printf("Format err..\n\tps:lnd srcDir objDir\n");
return -1;
}
DIR* srcDir = opendir(argv[1]);//open srcDir
if (srcDir == NULL)
{
printf("%s\n", strerror(errno));
return -2;
}
DIR* desDir = opendir(argv[2]);//open desDir
if (desDir == NULL)//ensure the desDir exist
{
char* desDir[128];
memset(desDir, 0, sizeof(desDir));
strcat((char*)desDir, "mkdir ");
strcat((char*)desDir, argv[2]);
strcat((char*)desDir, " -p");
system((char*)desDir);
}
else
{
closedir(desDir);
}
// link hard create
struct dirent* dient = readdir(srcDir);
char srcPath[128], desPath[128];
char cmd[256];
while (dient != NULL)
{
if (strcmp(dient->d_name,"..") != 0 && strcmp(dient->d_name, ".") != 0)
{
memset(srcPath, 0, sizeof(srcPath));
memset(desPath, 0, sizeof(desPath));
memset(cmd, 0, sizeof(cmd));
//-----------
strcat(srcPath, argv[1]);
if (argv[1][strlen(argv[1])] != '/')
strcat(srcPath, "/");
strcat(srcPath, dient->d_name);
//-----------
strcat(desPath, argv[2]);
if (argv[2][strlen(argv[2])] != '/')
strcat(desPath, "/");
strcat((char*)desPath, dient->d_name);
//strcat((char*)desPath, ".hard");
//-----------
strcat((char*)cmd, "ln ");
strcat((char*)cmd, srcPath);
strcat((char*)cmd, " ");
strcat((char*)cmd, desPath);
//-----------
system((char*)cmd);
//-----------
}
dient = readdir(srcDir);
}
closedir(srcDir);
return 0;
}
- 【Linux-ln拓展】可用于同时创建多个硬链接同时备份多个文件的程序
- Linux下同时wget多个文件
- linux下同时打开多个文件
- Delphi7同时保证多个Activex控件可用的办法
- linux ln 创建文件链接使用方法
- linux同时创建多进程的方法
- C#控制台 同时创建或者覆盖多个txt文件
- 同时删除多个文件
- 同时上传多个文件
- 同时上载多个文件
- linux链接文件 -- ln
- Linux链接文件ln
- linux cat 可以同时查看多个文件和过滤
- linux的建立文件链接命令ln
- Unix ln链接与链接文件备份
- 用批处理(bat)同时运行多个程序的方法
- Photoshop脚本 > 同时创建多个选区
- linux ln命令创建链接
- 常用adb指令
- java跳出外循环
- Java泛型:泛型类、泛型接口和泛型方法
- hdu-1878-欧拉回路(并查集||dfs)&&欧拉回路
- IntelliJ IDEA 自动导入包 快捷方式
- 【Linux-ln拓展】可用于同时创建多个硬链接同时备份多个文件的程序
- P3817_小A的糖果
- 百度前端技术学院_斌斌学院_task04
- 网易2018校招内推编程题_小易喜欢的数列
- Spark和hive整合的时候出现问题
- iOS @class与#import的区别
- 8.15 I
- 机器学习算法的要点(附 Python 和 R 代码)
- MVC设计模式