DU and Hard Links?

Wayne Davison wayned at samba.org
Sat Jun 19 20:22:13 GMT 2004

On Sat, Jun 19, 2004 at 01:55:02PM -0500, Max Kipness wrote:
> But what I would like to do is automatically get the size of the last
> created hard linked directory daily through a script.

Try the attached perl script.  It counts the blocks in the arguments you
specify (like du does), but it leaves out any files that have more links
than the number of instances we find in our scan (i.e. if we find "foo"
with a link count of 2 and we run across it again in our scan, we count
its blocks; if it had a link count of 3 or more and we only ran across
it twice, we'd skip it).

-------------- next part --------------
#!/usr/bin/perl -w

use strict;

my %hash; # Keep track of multi-linked inodes
my $total_blocks = 0;

my @todo = @ARGV;
push(@todo, '.') unless @todo;

foreach my $fn (@todo) {
    my($inode,$nlinks,$blocks) = (lstat($fn))[1,3,12];
    if (-d _) {
	opendir(DP, $fn) or die $!;
	push(@todo, map("$fn/$_", grep(!/^\.\.?$/, readdir(DP))));
	closedir DP;
    } elsif ($nlinks > 1) {
	if (!defined($hash{$inode})) {
	    $hash{$inode} = $nlinks - 1;
	next if --$hash{$inode};
    $total_blocks += $blocks;

print "$total_blocks blocks\n";
print int($total_blocks / 2 + .5), "K\n"; # Assumes 512-byte blocks

More information about the rsync mailing list