Category: | Utility Scripts |
Author/Contact Info | Neil Watson watson-wilson.ca |
Description: | Recover your disk space!
oldfiles searches a directory and sub directories for files of a certain age and size. A report is emailed to the owners of those files asking them to remove or archive them. This is a first draft. Everyone is encouraged to make comments and suggestions. Thank you. |
#!/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) }; } } } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: oldfiles
by graff (Chancellor) on Nov 15, 2002 at 04:10 UTC | |
by neilwatson (Priest) on Nov 15, 2002 at 13:58 UTC | |
by graff (Chancellor) on Nov 16, 2002 at 15:31 UTC | |
Re: oldfiles
by zentara (Cardinal) on Nov 15, 2002 at 14:17 UTC | |
Re: oldfiles
by AltBlue (Chaplain) on Nov 15, 2002 at 16:17 UTC | |
by neilwatson (Priest) on Nov 15, 2002 at 17:53 UTC | |
by AltBlue (Chaplain) on Nov 15, 2002 at 19:27 UTC |