views:

32

answers:

1

From http://en.wikipedia.org/wiki/Inode

Unix directories are lists of "link" structures, each of which contains one filename and one inode number.

I'd like to just get the length of this list of links, the names of the files in the directory aren't important at this point in my code.

A solution in Perl would be preferred, but any solution will do.

A: 
opendir DIR, '.';
$num_items = @{[readdir DIR]};
closedir DIR;

The central point is that readdir returns all the directory entries when called in list context.

Chris Jester-Young
I'm looking for a lower-level solution, since readdir is going to iterate over the contents of the directory.The Wikipeida article makes me thing that the directory structure itself might have some meta information like "there are 'n' elements here."
Trueblood
@Trueblood: No, there is not, at least not accessible in a portable way. You can, as a rough guide, use the size of the directory, but if (say) you put a million files in a directory (so it grows to size) then delete those files again, the directory isn't required to shrink back. In some filesystems, the enlarged size stays forever. :-)
Chris Jester-Young