在 Debian(或任何 Linux 系统)中,readdir 通常是 C 语言中用于读取目录的函数(opendir / readdir)。如果你说的是 用 readdir 统计目录中的文件数量,下面分几种常见情况说明。
. 和 ..)#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[]) {
DIR *dir;
struct dirent *entry;
int count = 0;
if (argc < 2) {
fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
return 1;
}
dir = opendir(argv[1]);
if (!dir) {
perror("opendir");
return 1;
}
while ((entry = readdir(dir)) != NULL) {
count++;
}
closedir(dir);
printf("Total entries: %d\n", count);
return 0;
}
✅ 编译:
gcc count.c -o count
./count /path/to/dir
. 和 ..while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
count++;
}
#include <sys/stat.h>
struct stat st;
char path[1024];
while ((entry = readdir(dir)) != NULL) {
snprintf(path, sizeof(path), "%s/%s", argv[1], entry->d_name);
if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
count++;
}
}
如果你不是写 C 程序,只是想在 Debian 上统计目录数量:
ls -1 | wc -l
find . -maxdepth 1 -type f | wc -l
find . -type f | wc -l
readdir 不会区分文件类型,需要配合 statreaddir 返回的是目录项(dirent),不是完整路径. 和 .. 也会被统计如果你是指:
可以告诉我,我可以给你更精确的示例。