在Debian中,你可以使用C语言的dirent.h
库中的readdir
函数来读取目录中的文件。要获取文件的修改时间,你需要使用sys/stat.h
库中的stat
结构体。以下是一个简单的示例,展示了如何使用readdir
和stat
获取目录中文件的修改时间:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <directory>\n", argv[0]);
return 1;
}
DIR *dir = opendir(argv[1]);
if (dir == NULL) {
perror("opendir");
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) { // 只处理普通文件
struct stat file_stat;
char file_path[PATH_MAX];
snprintf(file_path, sizeof(file_path), "%s/%s", argv[1], entry->d_name);
if (stat(file_path, &file_stat) == -1) {
perror("stat");
continue;
}
char time_buf[26];
ctime_r(&file_stat.st_mtime, time_buf);
time_buf[strcspn(time_buf, "\n")] = 0; // 去掉换行符
printf("%s: %s", entry->d_name, time_buf);
}
}
closedir(dir);
return 0;
}
这个程序接受一个目录作为命令行参数,然后使用readdir
函数读取目录中的文件。对于每个文件,它使用stat
函数获取文件的元数据,包括修改时间。然后,它使用ctime_r
函数将修改时间转换为可读的字符串格式,并将其打印出来。
要编译此程序,请将其保存为list_modified_time.c
,然后在终端中运行以下命令:
gcc -o list_modified_time list_modified_time.c
现在你可以使用这个程序来获取Debian中目录里文件的修改时间。例如:
./list_modified_time /path/to/directory