在 Ubuntu(Linux)下,如果你说的 “copendir” 是指 opendir / 目录遍历相关编程(C 语言),或者是 命令行递归查找目录,下面分两种情况说明。
find 递归查找目录find /path/to/search -type d -name "目录名"
示例:递归查找名为 test 的目录
find . -type d -name "test"
find . -type d -name "*log*"
find . -type d -iname "*log*"
find . -type d
opendir 递归查找目录如果你是 写 C 程序,用 opendir / readdir 实现递归遍历:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void list_dir(const char *path) {
DIR *dir = opendir(path);
struct dirent *entry;
if (!dir) return;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
char fullpath[1024];
snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entry->d_name);
struct stat st;
stat(fullpath, &st);
if (S_ISDIR(st.st_mode)) {
printf("目录: %s\n", fullpath);
list_dir(fullpath); // 递归
}
}
closedir(dir);
}
int main() {
list_dir(".");
return 0;
}
编译:
gcc find_dir.c -o find_dir
./find_dir
scandir / nftwnftw():更标准的递归目录遍历scandir():一次性读取目录如果你愿意,可以告诉我:
我可以给你更精确的写法。