#include<dirent.h>
#include<string.h>
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
struct F //结构体F,保存有文件描述符,文件名,文件的绝对位置
{
int id;//文件描述符
char name[512];//文件名
char path[512];//绝对路径
int t;//是否被删除,1存在,0删除;
};
int List(char *path,F file[],int n)
{
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
char tmpdir[512];
struct stat statbuf;
tmpdir[0]='\0';
if((pDir=opendir(path))==NULL)
{
fprintf( stderr, " 目录%s打开失败!\n", path );
return 0;
}
while((ent=readdir(pDir))!=NULL)
{
lstat(dir, &statbuf);
if(S_ISDIR(statbuf.st_mode))//statbuf.st_mode文件的模式 文件or目录
continue;
else
{
if (strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0) continue;
printf( " 文件:%s \n", ent->d_name );
strcat(tmpdir,path);
strcat(tmpdir,ent->d_name);
tmpdir[strlen(tmpdir)]='\0';
n++;
if (file[n].id=open(tmpdir,O_RDWR)==-1)
{
printf("open file Error!\n");
return 0;
}
file[n].id=open(tmpdir,O_RDWR);
file[n].name[0]='\0';
strcat(file[n].name,ent->d_name);
file[n].path[0]='\0';
strcat(file[n].path,path);
strcat(file[n].path,ent->d_name);
file[n].path[strlen(file[n].path)]='\0';
file[n].t=1;
printf("%s\n",file[n].path);
tmpdir[0]='\0';
}
}
closedir(pDir);
return n;
}
void find(F file[],int n,char path)//利用选择排序的方式,对任意两个文件进行比对(获得两个文件描述符,调用compare函数进行比较)
{
int compare(F f1,F f2);
int i,j;
int choose;
int temp[n];
char tmp1[512];
char tmp2[512];
printf("正在凶残地比计算中....\n");
printf("请稍后....\n");
for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
{
if (file[i].t!=0 && file[j].t!=0)
if (compare(file[i],file[j])==1)
{
close(file[i].id);
close(file[j].id);
open(file[i].path,O_RDWR);
open(file[j].path,O_RDWR);
continue;
}
else
{
close(file[i].id);
close(file[j].id);
open(file[i].path,O_RDWR);
open(file[j].path,O_RDWR);
printf("\n相同文件:%s %s\n",file[i].name,file[j].name);
printf("位置:%s\n",file[j].path);
tmp1[0]='\0';
strcat(tmp1,"rm ");
strcat(tmp1,"");
strcat(tmp1,file[j].path);
tmp2[0]='\0';
strcat(tmp2,"rm ");
strcat(tmp2,"");
strcat(tmp2,file[i].path);
printf("删除该文件(1/2/3)?\n1.%s 2.%s 3.保留两个文件\n",file[j].name,file[i].name);
scanf("%d",&choose);
if (choose==1) {printf("%s\n",tmp1);system(tmp1);file[j].t=0;printf("删除成功!\n");}
else if (choose==2) {printf("%s\n",tmp2);system(tmp2);file[i].t=0;printf("删除成功!\n");}
else printf("鄙人那么辛苦的计算出来,你就给我保留了……尔等莫不是来消遣洒家?!\n");
}
}
}
int compare(F f1,F f2)//比较两个文件(文件描述符)的内容是否相同,相同返回0,不相同返回1;
{
char buffer1;
char buffer2;
int n=1;
int a,b;
//printf("比较中的文件:%s %s\n",f1.name,f2.name);
while (1)
{
a=read(f1.id,&buffer1,1);
b=read(f2.id,&buffer2,1);
//printf("读取字节数:%d %d\n",a,b);
//printf("读取字符:%c %c\n",buffer1,buffer2);
if (a==0 && b==0)
{
close(f1.id);
close(f2.id);
open(f1.path,O_RDWR);
open(f2.path,O_RDWR);
//printf("*******%s %s相同返回\n\n",f1.name,f2.name);
return 0;
}
if (a*b<0)
{
close(f1.id);
close(f2.id);
open(f1.path,O_RDWR);
open(f2.path,O_RDWR);
//printf("长度不同返回\n\n");
return 1;
}
if (buffer1!=buffer2)
{
close(f1.id);
close(f2.id);
open(f1.path,O_RDWR);
open(f2.path,O_RDWR);
//printf("出现不同返回\n\n");
return 1;
}
}
close(f1.id);
close(f2.id);
open(f1.path,O_RDWR);
open(f2.path,O_RDWR);
//printf("相同返回\n\n");
return 0;
}
int main(int argc, char* argv[])
{
F file[100];
int n=0;
if(argc == 2)
n=List(argv[1],file,0);
else printf("shell code ERROR!\n");
printf("******%d个文件*******\n\n",n);
find(file,n,*argv[1]);
for (int i=1;i<=n;i++)
close(file[i].id);
printf("\n___________________________________\n\n任务完成!\n感谢使用\n ——北冥孤隐\n\n时间紧迫,功能单一,见谅~\n———————————————————————————————————\n\n");
return 0;
}
rm.rar_LINUX 遍历文件_linux rm_rm_rm linux_rm2013目录
版权申诉
41 浏览量
2022-09-24
05:36:42
上传
评论
收藏 9KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- 3122080306 邹子轩 实验报告二.docx
- 基于STM32 NUCLEO板设计彩色LED照明灯(纯cubeMX开发)(大赛作品,文档完整,可直接运行)
- 发那科工业机器人保养大全
- Sphere.h
- REMD固有时间尺度分解信号分量可视化(Matlab完整源码和数据)
- 嵌入式系统双单片机STC89C52+STC15W104多功能学习板电路图可扩展 适用于单片机初学者和教学
- 基于STM32蓝牙控制小车系统设计(硬件+源代码+论文)大赛作品
- XILINXFPGA源码基于Spartan3火龙刀系列FPGA开发板VGA测试例程
- Java聊天室的设计与实现【尚学堂·百战程序员】
- python中matplotlib教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈