With what little knowledge I have of Perl syntax combined with a wee bit of cannibalism, I hacked together this script that will delete images older than X number of days from a certain directory and log the results ...
code:
#!/usr/bin/perl -l
$Directory = "/home/user/directory/files";
$LogFile = "/home/user/directory/cleanup.log";
$Days = 7;
$Seconds = $Days * 86400;
$Date = localtime (time);
$Deleted = 0;
$Skipped = 0;
# Double >> will append file if it exists. Single > overwrites.
open(OUTPUTFILE, ">>$LogFile") || die "open0: $!";
print "\n==============================";
print "Checking files in directory \"$Directory\".";
print "Files older than $Days days will be deleted.";
print "----------------------\n";
print OUTPUTFILE "\n========== Open Log ($Date) ==========\n";
print OUTPUTFILE "Checked files in directory \"$Directory\".";
print OUTPUTFILE "Files older than $Days days were deleted.";
print OUTPUTFILE "----------------------\n";
for (glob("$Directory/*")) {
if (-f && (time - (stat($_))[9] >= $Seconds)) {
unlink;
print " DELETING: $_";
print OUTPUTFILE " DELETED: $_";
$Deleted++;
} else {
print " Skipping: $_";
print OUTPUTFILE " Skipped: $_";
$Skipped++;
}
}
if ($Deleted < 1) {
print "\n NO FILES FOUND OLDER THAN $Days DAYS";
print OUTPUTFILE "\n NO FILES FOUND OLDER THAN $Days DAYS";
}
print "\n----------------------";
print "$Deleted files were deleted.";
print "$Skipped files were skipped.";
print "==============================\n";
print OUTPUTFILE "\n----------------------";
print OUTPUTFILE "$Deleted files were deleted.";
print OUTPUTFILE "$Skipped files were skipped.\n";
print OUTPUTFILE "======================= Close Log =======================\n\n";
close(OUTPUTFILE) || die "close0: $!";
I have only one annoyance: Because I'm running this as a cron job, I have to specify my entire user directory path, which is actually much longer then the dummy one I put up there. Thus, the entire directory path gets output in the log, like so:
quote:
========== Open Log (Sun Oct 3 14:11:15 2004) ==========
Checked files in directory "/home/user/directory/files".
Files older than 7 days were deleted.
----------------------
DELETED: /home/user/directory/files/old.txt
Skipped: /home/user/directory/files/young.txt
DELETED: /home/user/directory/files/older.txt
----------------------
2 files were deleted.
1 files were skipped.
======================= Close Log =======================
Now, I wouldn't mind keeping the entire directory path in the "Checked files in directory X" statement, but is there any way I can list only the file names in the DELETED/Skipped list?
By the way, for anyone wishing to use this script, you can test it by creating old/new files with the following commands:
quote:
touch -m 01020001 old.txt (Creates a file with the modified date of Jan. 2, 12:01 a.m.)
touch -m 01010001 older.txt (Creates a file with the modified date of Jan. 1, 12:01 a.m.)
touch -m new.txt (Creates a file with the current date/time. Yeah, there's probably a more appropriate way to do this.)