Why should the data structure contain a number? A tree does not need to know its size in O (1), unless this is a requirement (and ensuring this may require more blocking and possibly a performance bottleneck)
By tree, I do not mean including the contents of subdir, but files with -maxdepth 1 - suppose that they really are not stored in the list.
edit: ext2 saved them as a linked list.
modern ext3 implements hashed B-Trees
Having said that, / bin / ls does a lot more than counting and actually scans all inodes. Write your own C program or script using opendir () and readdir ().
:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main()
{
int count;
struct DIR *d;
if( (d = opendir(".")) != NULL)
{
for(count = 0; readdir(d) != NULL; count++);
closedir(d);
}
printf("\n %d", count);
return 0;
}