#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <dirent.h>
#define MON 2678400
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
dp = opendir("./");
struct stat stbuf;
long now;
time(&now);
int m = 6;
if (argc > 1)
m = atoi(argv[1]);
if (m == 0)
{
printf("usage: clean months\n");
return 0;
}
int total = 0;
while ((dirp = readdir(dp)) != NULL)
{
char *filename = dirp -> d_name;
if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
continue;
stat(filename, &stbuf);
if (now - stbuf.st_mtime <= MON * m)
continue;
remove(filename);
total++;
}
closedir(dp);
printf("clean: remove %d files\n", total);
return 0;
}
删除当前目录n个月前的文件
最新推荐文章于 2021-08-30 15:37:00 发布