在 Debian(或者说 Linux 下 C/C++ 编程)里,readdir 本身不能过滤,它只是逐个返回目录项。
readdir 本身struct dirent *readdir(DIR *dirp);
struct dirent *例如只保留普通文件:
while ((entry = readdir(dp)) != NULL) {
if (entry->d_type == DT_REG) {
printf("%s\n", entry->d_name);
}
}
if (strstr(entry->d_name, ".log")) { ... }
scandir(推荐,支持过滤函数)struct dirent **namelist;
int n = scandir(".", &namelist, filter, alphasort);
int filter(const struct dirent *d) {
return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
}
scandir 可以传入 过滤函数ls *.txt
这是 shell / glob 做的,不是 readdir
| 方式 | 是否能过滤 |
|---|---|
readdir |
❌ 不能,只能遍历 |
scandir |
✅ 支持过滤函数 |
| 自己判断 | ✅ 最灵活 |
| shell / glob | ✅ 但不是系统调用 |
如果你说的是 某个具体语言(Python / C++ / Rust) 或 某种过滤规则(后缀 / 权限 / 时间),可以告诉我,我给你示例。