Andy Westlake <[EMAIL PROTECTED]> wrote:

> Bit of an odd one but I need to know how many regular files there are on our
> system so I knocked together a little script, see below.  Only problem is it
> seems to run out of memory as its running.
> Any thoughts on how I could improve it would be very gratefully received, or
> is there a module I don't know about that will help?
......

Below is my suggestion.

Vinicius


#!/usr/bin/perl
#
# Copyright (C) 2001 Andy Westlake <[EMAIL PROTECTED]>
#
# /usr/local/sysadm/system/filecount.pl
#
# Usage ./filecount.pl
#
# Script to count all the files on the system with the exception
# of the /, usr and var file systems.
#
# Who   Date            Modifications
# ===   ====            =============
# AGJW  04-Jul-2001     Initial script.
# VJL   30-aug-2001     Some improvements.
#

use File::Find;
use strict;

# could be passed by command line option like: -size 1000
my $minimum_size = 0;
# suggestion: could have a command line option like: -orderby {size|name}
#             default: name
my $order_by = 'name';

# ---------------------------------------------
# Set up variables.
# ---------------------------------------------
my $date_extn = `date +%Y-%m-%d-%R`;
chomp ($date_extn);
my $logfile = "filecount.$date_extn";
### my $logfile = "/usr/local/sysadm/system/filecount.$date_extn";

open (FSTAB, "</etc/fstab")
    or die "Can't read from /etc/fstab: $!, stopped";
my %fstab;
while (<FSTAB>) {
    my $data = (split)[1];
    $fstab{$data} = 1
        if (defined $data && ($data !~ m=^/proc$|^/$|^/var$|^/usr$=));
}
close (FSTAB);

open (LOGFILE, ">$logfile")
    or die "Can't write to $logfile: $!, stopped";
my %wanted;
my $totfiles = 0;
foreach (sort keys %fstab) {
    print "Mount point is $_\n";
    print LOGFILE "Mount point is $_\n";
    %wanted = ();
    find (\&wanted1, $_);
    foreach (($order_by eq 'name')
             ? (sort keys %wanted)
             : (sort {$wanted{$b} <=> $wanted{$a}} keys %wanted)) {
        printf LOGFILE "   %7d  $_\n", $wanted{$_}
            if ($wanted{$_} >= $minimum_size);
    }
}
print LOGFILE "\n\nTotal number of files is $totfiles\n";
close (LOGFILE);

# ---------------------------------------------
# Define sub-routines.
# ---------------------------------------------
sub wanted1 {
    return if (($_ eq ".") || ($_ eq ".."));
    return unless -d $_;
    my $regfiles = 0;
    opendir (CURDIR, $File::Find::name);
    while (my $name = readdir (CURDIR)) {
        $regfiles += 1
            unless ((-d $name) || (-l $name));
    }
    closedir (CURDIR);
    $wanted{$File::Find::name} = $regfiles;
    $totfiles += $regfiles;
}


### filecount.pl ends here

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to