Here's a simple Perl script that'll do it:
#!/usr/bin/perl -w
use strict;
sub count_inodes($);
sub count_inodes($)
{
my $dir = shift;
if (opendir(my $dh, $dir)) {
my $count = 0;
while (defined(my $file = readdir($dh))) {
next if ($file eq '.' || $file eq '..');
$count++;
my $path = $dir . '/' . $file;
count_inodes($path) if (-d $path);
}
closedir($dh);
printf "%7d\t%s\n", $count, $dir;
} else {
warn "couldn't open $dir - $!\n";
}
}
push(@ARGV, '.') unless (@ARGV);
while (@ARGV) {
count_inodes(shift);
}
If you want it to work like du
(where each directory count also includes the recursive count of the subdirectory) then change the recursive function to return $count
and then at the recursion point say:
$count += count_inodes($path) if (-d $path);