Since I'm having performance problems with rsnapshot, I'd like to identify directories with great number of files recursively. I think the problem is not the size of the files, but the file count in particular subdirectories, because the generations (daily.0, daily.1, ...) are not volatile and only have few changes compared to the total number of files.Unix command du would be exactly what I want, if it returned only the file count and not the sum of file sizes.I already have a bash script which outputs the file count of all direct subdirectories (recursing into subdirectories), but it's cumbersome to use, because I have to dig deeper and deeper and always have to wait.Found also a script digging deeply, but not summing up file count of subdirectories. It shows only the number of files in this directory, not from its children.Doesn't have to be a shell script - I'm open to other scripting languages like Ruby, Python, Perl, JavaScript, ...Example:dir1/   file1   subdir1/       file2, file3, file4, file5   subdir2/       file6, file7, file8       subdir3/           file9dir2/    fileA, fileBDesired output (listing subdirectories and sum up to top):4   dir1/subdir11   dir1/subdir2/subdir34   dir1/subdir29   dir1/2   dir2/What I don't want (only listing totals):9   dir1/2   dir2/and not (only listing file count of . directory):4   dir1/subdir11   dir1/subdir2/subdir33   dir1/subdir21   dir1/2   dir2/
Get recursive file count (like `du`, but number of files instead of size)
files;disk usage
Try something like this:find . -type f | perl -aF/ -lne 'for (my $i=0; $i < @F-1; ++$i) { print join(/,@F[0...$i]); }' | sort | uniq -cfind . -type f prints files:./dir1/subdir2/file8./dir1/subdir2/file7./dir1/subdir2/subdir3/file9./dir1/subdir2/file6./dir1/file1...perl -aF/ -lne 'for (my $i=0; $i < @F-1; ++$i) { print join(/,@F[0...$i]); }' translates each filename ./a/b/c to a set of directories ., ./a, ./a/bNote:doesn't work with newlines in filenames. You can use -print0 in find, -0 in perl, and put counters for each directory in hash.Edit:Inspired by @Gilles's answer:find . -depth -print0 |perl -0 -ne 'my $depth = tr!/!/!;for (my $i = $prev_depth; $i <= $depth; ++$i) { $totals[$i] = 0; }if ( -f $_ ) {  for (my $i = 0; $i <= $depth; ++$i) { ++$totals[$i]; }} else {  print $totals[$depth]\t$_\n;}$prev_depth = $depth;'Works fine with newlines in filenames. Works fine with empty directories. Doesn't require additional sort | uniq -c.