linux删除文件个数,c语言删除linux指定文件夹下指定数量的文件

#include#include#include

#define MAXLEN 1024typedefstructfile_info{char permission[50];char linkNum[10] ;char owner[50];char group[50];char size[20];char month[50];char day[50];char time[50];char fileName[100];

} FILE_INFO;structlist{intindex;

FILE_INFO fileInfo;/*链表中包含的元素*/

struct list *next; /*指向下一个链表的指针*/};/*定义一个链表头部*/

static struct list *list_head =NULL;void rmFile(char * path,intdelNum);int getFileList(char *dir);static void list_add(struct list **head, struct list *list);static void list_print(struct list **head);static void list_free(struct list **head);intmain(){char path[]="/home/woxin/test2";//文件夹

int delNum = 3;//要删除的数量

rmFile(path,delNum);

}void rmFile(char * path,intdelNum){char str[100] = {0};int flag = 0;int fileNum =getFileList(path);struct list *temp ;

temp=list_head;

printf("要删除的文件夹:%s\n",path);

printf("文件夹下的文件数:%d\n",fileNum);

printf("要删除的文件数量:%d\n",delNum);int k = 0;int count=0;for(;k

temp= temp->next; //**********************这里虽然写出来了,但是不明白为什么要加这一句。*******************

if(temp){char cmd[100] = "rm -rf";

strcat(cmd,path);

strcat(cmd,"/");

strcat(cmd,temp->fileInfo.fileName);

printf("执行的命令:%s\n",cmd);

system(cmd);

printf("fileName: %s is remove !\n", temp->fileInfo.fileName);

temp= temp->next;

count++;

}if(count==delNum) break;

}int fileNum2 =getFileList(path);

printf("删除后文件的数量:path:%d\n",fileNum2);

list_free(&list_head);

}int getFileList(char *dir){//ls -rlct

FILE *f;char cmd[]="ls -rlct";

strcat(cmd,dir);charbuffer[MAXLEN];char null[20];

FILE* pipe = popen(cmd, "r");if (!pipe) return -1;int i=0;for(;fgets(buffer, sizeof(buffer), pipe)!=NULL;i++){

FILE_INFO file;

sscanf(buffer,"%s %s %s %s %s %s %s %s %s",file.permission,file.linkNum,file.owner,file.group,file.size,file.month,file.day,file.time,file.fileName);struct list * mylist =NULL;

mylist= malloc(sizeof(structlist));

mylist->fileInfo =file;

mylist->index =i;

list_add(&list_head, mylist);//free(mylist);

}

pclose(pipe);return i-1;

}//添加一个节点

static void list_add(struct list **head, struct list *list) {struct list *temp;/*判断链表是否为空*/

if(NULL == *head) {/*为空*/

*head =list;

(*head)->next =NULL;

}else{/*不为空*/temp= *head;while(temp)

{if(NULL == temp->next)

{

temp->next =list;

list->next =NULL;

}

temp= temp->next;

}

}

}//打印所有节点

static void list_print(struct list **head) {struct list *temp;

temp= *head;

printf("list information :\n");while(temp)

{

printf("\t %d permission: %s\n", temp->index, temp->fileInfo.permission);

printf("\t linkNum: %s\n", temp->fileInfo.linkNum);

printf("\t owner: %s\n", temp->fileInfo.owner);

printf("\t group: %s\n", temp->fileInfo.group);

printf("\t size: %s\n", temp->fileInfo.size);

printf("\t month: %s\n", temp->fileInfo.month);

printf("\t day: %s\n", temp->fileInfo.day);

printf("\t time: %s\n", temp->fileInfo.time);

printf("\t fileName: %s\n", temp->fileInfo.fileName);

temp= temp->next;

}

}//释放所有节点

static void list_free(struct list **head) {struct list *temp = *head;while(temp) {struct list *p = temp->next;free(temp);

temp=NULL;

temp=p;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值