Link to home
Start Free TrialLog in
Avatar of KCarter80
KCarter80

asked on

Need a Perl script to delete files older than 10 days in specified directory (Redhat machine)

This is close, but it deletes everything =).

$BACKUP_DIR = "/var/www/backup";

# Read directory (ignoring . and ..)
opendir(DIR,"$BACKUP_DIR") || die "opendir $BACKUP_DIR failed";
@files = grep(!/^\.\.?$/,readdir(DIR));
closedir(DIR);

# delete all files in directory
foreach $f (@files)
{
      system("rm $BACKUP_DIR/$f");
}
Avatar of GnarOlak
GnarOlak

The -A file test operator returns the interval from the date the file was last modified in days.  Test for that value being greater than the age at which you want to delete the file.

For example, you could do this:

system("rm $BACKUP_DIR/$f") unless (-A $BACKUP_DIR/$f < 30);
or this
system("rm $BACKUP_DIR/$f") if (-A $BACKUP_DIR/$f >= 30);

or any number of other logical constructs.

This will only delete files that are 30 or more days old.

I tested the -A operator on a Windows machine using a file last modified on 4/11/2006 and it returned 30.0196875.

Here is the trivial code I used to test this:

$age = -A 'file.txt';
print $age, "\n";
Change your foreach loop like so.  Of course, test it on non-critical files first!


foreach $f (@files)
{
    next if (time()-(stat($f))[9]) < 10*24*60*60;
    system("rm $BACKUP_DIR/$f");
}
Correction.  -A tests for access time.  -M will check for modification time.
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perl -e 'unlink grep{-M > 10}</var/www/backup/*>;'
my $BACKUP_DIR = "/var/www/backup";

# Read directory (ignoring . and ..)
opendir(DIR,"$BACKUP_DIR") || die "$!!";
my @files = grep(!/^\.\.?$/,readdir(DIR));
closedir(DIR);

# delete all files in directory
for (@files) {
#lets put a brake or two on here, and match stuff
#we DEFINATELY don't wanna delete...
next unless $_ =~ /some_match/gi;

my $del = join("","$BACKUP_DIR","$_");
unlink $del;
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The "find" command is a lot simpler and better suited for this type of operation. Try:
"find /var/www/backup -mtime +9 -exec {}  \;" on the command line.

If you really, really, really need it to be in perl, then:

#!/usr/bin/perl
`find /var/www/backup -mtime +9 -exec {}  \;`;
exit;

Tintin's solution
find /var/www/backup -type f -maxdepth 1 -mtime +9 -exec rm -f {} \;

FishMonger's solution
perl -e 'unlink grep{-M>10}</var/www/backup/*>'

jpiskor's solution
find /var/www/backup -mtime +9 -exec {}  \;  #  Oops, you forgot to delete the files, and this desends into sub dirs.

adjusted to delete the files, but based on the question, it should only be applied to a specified directory excluding sub dirs.
find /var/www/backup -mtime +9 -exec rm -f {}  \;

If we add the directory desent restriction, we're back to Tintin's solution.

All-in-all, the perl solution would be easiest.