#!/usr/bin/perl use strict; use warnings; use File::Find; use Time::Local; use Getopt::Std; use Mail::Sender; my $sender = new Mail::Sender; #Neil H Watson on Tue Mar 26 13:34:29 EST 2002 #usage oldfiles -d <directory> -a <days old> <option> #option -s <size in megabytes> reports only file larger than my %opt= ( #set default options d => ".", a => 0, s => 0, ); getopts("d:a:s:", \%opt); #: means string to follow if ($opt{a} == 0){ print <<"*END*"; usage oldfiles2 -d <directory> -a <days old> <option> option -s <size in megabytes> reports only file larger than Report of matching files will be emailed to the owners *END* } my ($uid, %files, $size, @pw, %unames, $str); my $host = `/bin/hostname`; my $domain = "mydomain.com"; my $support = "support\@mydomain.com"; my $from = "support\@mydomain.com"; #get user ids and names open PW, "/etc/passwd"; while (<PW>){ @pw = split ":", $_; #unames key=id value is username $unames{$pw[2]} = $pw[0] || "nwatson"; #print "id = $pw[2], name = $pw[0]\n"; } close PW; find(\&wanted, $opt{d}); foreach $uid (keys %files) { #construct email $sender->Open({ smtp => 'mail', to => "$unames{$uid}\@$domain", from => "$from", subject => "Disk cleanup needed on $host", headers => "Errors-To: postmaster\@$domain"}); die "Error: $Mail::Sender::Error\n" unless ref $sender; $sender->Body; $sender->SendLineEnc(<<"*END*"); The following files are listed as belonging to you and have not been a +ccessed in some time. If they are not needed please delete them. If + you require long term, static storage of these files please email $s +upport. Thank you. Files: *END* $str = sprintf "%11s%11s\tFile Name","Size(MB)","Age(Days)"; $sender->SendLineEnc("$str"); #cycle through file list of user and create #line in email body foreach my $file (@{$files{$uid}}) { $str = sprintf "%11s%11s\t$file->{name}",$file->{size},$file-> +{age}; $sender->SendLineEnc("$str"); } $sender->Close; } sub wanted{ #checks for old files if (-r && -f){ #get size of file (MB) $size=int((lstat($_))[7]/1000000); # check the age of file if ($opt{a} < -A && $size > $opt{s}){ $uid = (lstat($_))[4]; #use a hash of an array of hashes :) #hash of uid, that points to an array #whose elements hold the hashes for #name, size, age push @{$files{$uid}}, { name => $File::Find::name, size => $size, age => int(-A) }; } } }

In reply to oldfiles by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.