在 Debian(以及所有遵循 POSIX 标准的 Linux 系统)里,opendir 本身不能遍历目录,它只是打开目录,真正“遍历”要靠 readdir。
#include <dirent.h>
DIR *opendir(const char *name); // 打开目录,返回 DIR*
struct dirent *readdir(DIR *dirp); // 读取下一个目录项
int closedir(DIR *dirp); // 关闭目录
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[]) {
const char *path = argc > 1 ? argv[1] : ".";
DIR *dir = opendir(path);
if (!dir) {
perror("opendir");
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return 0;
}
opendir:只负责打开目录,拿到一个 DIR*readdir:每次调用返回一个目录项(.、..、文件、子目录等)opendir + readdir 循环 + closedird_type == DT_DIR 并再次 opendir<filesystem>(C++17)ls -R 或 find如果你是想问 “Debian 下有没有命令或函数能直接遍历目录”,也可以告诉我你用的是 C / C++ / Shell / Python,我可以给更具体的例子。