Gossamer Forum
Home : General : Perl Programming :

delete files in directory based on last modified date

Quote Reply
delete files in directory based on last modified date
Wondering if anyone can help with what I think should be a simple problem... I need a simple perl command to delete all files in a directory that were last modified before a particular date (say over a month ago). Anyone care to help? Many thanks.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] delete files in directory based on last modified date In reply to
Hmm... I'd have thought this would be relatively easy, but maybe it's harder than I realized.

The script I've got now (which is a super-simple auto-backup script) mostly does it's work through basic shell commands that are passed with backticks. So, for example, the backup archiving itself is accomplished by:

`tar cf $archive $target_dir --exclude $exclude_dir`;
`bzip2 $archive`;

Seems like I should be able to do something similar using ls, but I just don't know enough about perl to know how to pass the information from that command to a perl array or hashref including last modified dates. Any suggestions at all? Thanks very much.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] delete files in directory based on last modified date In reply to
This worked for me, but had to click refresh to delete each file. So I tried putting it in a loop, but I ran out of time.

Code:
#!/usr/local/bin/perl

print "Content-Type: text/html;\n\n";
print "<HTML><BODY>";
opendir(DELE, './test') or die "Could not open directory\n";
print "directory opened";
@TODELETE = readdir(DELE);
chdir('./test') or die "Could not change directories\n";

#PUT LOOP HERE

foreach $Listing (@TODELETE){
if (-d $Listing) {next}
if ((-C $Listing) < 10) {
unlink($Listing);
print "<BR>removed $Listing\n\n";
}
}

#END LOOP HERE

closedir(DELE);
print "<P>directory closed";
print "</HTML></BODY>";

Good Luck!
Quote Reply
Re: [Watts] delete files in directory based on last modified date In reply to
BTW, use "-M" for modification age in days here:

if ((-C $Listing) < 10) {

The -C is for creation date/age in days.
Quote Reply
Re: [Watts] delete files in directory based on last modified date In reply to
Thanks, Watts! That was the help I needed.

Here's what I've got now, which seems to work fine:

Code:

#!/usr/bin/perl -w
use strict;

&MAIN();

sub MAIN
{
my $target_dir = '/PATH/TO/DIRECTORY';
my $backup_dir = '/PATH/TO/STORE/ARCHIVE';
my $exclude_dir = '/PATH/TO/EXCLUDE';
my $days_to_keep = 21;
my $archive;

# Move to the backup directory
chdir($backup_dir) or die "ERROR: Could not change to backup directory\n";
if (! -d $target_dir)
{
die "ERROR: Target is not a directory.\n";
}

if (! -r $target_dir)
{
die "ERROR: Can't read target.\n";
}

# Make archive name
$archive = makeArchiveName($target_dir);

# Make the tarball
`tar cf $archive $target_dir --exclude $exclude_dir`;

# Bzip the tarball
`bzip2 $archive`;

# Delete old archives
opendir(DELE, $backup_dir) or die "Could not open directory\n";
@TODELETE = readdir(DELE);
foreach $archive_file (@TODELETE) {
if(-d $archive_file) {next};
if ((-C $archive_file) > $days_to_keep) {
unlink($archive_file);
}
}
closedir(DELE);
}

sub makeArchiveName
{
## stuff in here to generate the archive file name
}

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund