du -dirs

Kevin Buettner kev@primenet.com
Thu, 22 Jun 2000 00:02:13 -0700


On Jun 21, 10:16pm, der.hans wrote:

> anyone know of a way to get du to give me the cumulative size of the files
> in a dir tree, but to not include the dir sizes? Or how to get the
> cumulative size of the dirs and not include files?
> 
> I've got a tarball that opens to a known size on one box and another size
> on another. The 1st box has 1024 dirs and the 2nd 4096 dirs. I presume
> this is the difference, but it's 10MB on a 100MB tarball, so I'd like to
> be certain I'm not losing 10MB of stuff :). I'm also going to be comparing
> dus of the filesystem on both boxen as part of my backup mechanism and I'd
> like to be able to automagically verify that everything's ok.
> 
> Granted, I could write a script that would do this, but that would require
> that I think as well as put together 10 or 20 lines of code. Why should I
> go to soooo much effort if there's already a tool to do this? ;-)

I'm sure there's some obscure set of switches to do this, but we
both know that it's *way* more fun to write a script, particularly
if it's in perl.

The script below will give you a count of the regular files and also
provide you with the sum of their sizes.  It doesn't take into account
the space wasted by partially using a disk block, but then you don't
want that for your purposes.

Here's how you use it:

ocotillo:ptests$ ./fsizes /usr/local
/usr/local: 1973 files; 110960876 bytes total

--- fsizes ---
#!/usr/bin/perl -w

use File::Find;

my ($root) = @ARGV;

if (!defined($root)) {
    die "Usage: $0 root\n";
}

my ($size, $count) = (0, 0);

find(
    sub { 
	if (-f && !-l) {
	    $size += -s;
	    $count++;
	}
    },
    $root
);

print "$root: $count files; $size bytes total\n";
--- end fsizes ---