BASH
find / -xdev -printf ‘%h\n’ | sort | uniq -c | sort -k 1 -n
PYTHON
import sys
import os
dir = sys.argv[1]
subdirs = os.listdir(dir)
notparse = «/proc,/dev,/sys»
def walk(fsubdir):
try:
global count
for name in os.listdir(fsubdir):
path = os.path.join(fsubdir, name)
if os.path.isfile(path):
count = count + 1
else:
count = count + 1
walk(path)
except:
pass
for subdir in os.listdir(dir):
fsubdir = os.path.join(dir,subdir)
count = 0
if os.path.isdir(fsubdir) and fsubdir not in notparse:
walk(fsubdir)
print fsubdir,count
PERL
#! /usr/bin/perl -w
# Written by Kjetil Torgrim Homme <kjetil.homme@redpill-linpro.com>
use strict;
use File::Find;
my %counted;
my %total;
sub count {
++$counted{$File::Find::dir};
}
sub exeunt {
my $dir = $File::Find::dir;
# Don’t report leaf directories with no files
return unless $counted{$dir};
my $parent = $dir;
$parent =~ s!/[^/]*$!!;
$total{$dir} += $counted{$dir};
$total{$parent} += $total{$dir} if $parent ne $dir;
printf(«%8d %8d %s\n», $total{$dir}, $counted{$dir}, $dir);
delete $counted{$dir};
delete $total{$dir};
}
die «Usage: $0 [DIRECTORY…]\n» if (@ARGV && $ARGV[0] =~ /^-/);
push(@ARGV, «.») unless @ARGV;
finddepth({ wanted => \&count, postprocess => \&exeunt}, @ARGV);