[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14811
  • Last Modified:

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");
}
0
KCarter80
Asked:
KCarter80
2 Solutions
 
GnarOlakCommented:
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";
0
 
Adam314Commented:
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");
}
0
 
GnarOlakCommented:
Correction.  -A tests for access time.  -M will check for modification time.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TintinCommented:
Why use Perl?

find /var/www/backup -type f -maxdepth 1 -mtime +9 -exec rm -f {} \;
0
 
FishMongerCommented:
perl -e 'unlink grep{-M > 10}</var/www/backup/*>;'
0
 
kblack05Commented:
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;
}
0
 
FishMongerCommented:
The simple command line version I gave would be the easiest and shortest, but if this is part of a larger script, you can use the same command in the script.

#!/usr/bin/perl -w

use strict;

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

unlink grep{-M > 10}<$BACKUP_DIR/*>;

----------

However, you may want to print out the filenames as they're being deleted.

#!/usr/bin/perl -w

use strict;

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

my @files =  grep{-M > 10}<$BACKUP_DIR/*>;

for (@files) {
   print "Deleting $_ ... ";
   if (unlink $_) {
      print "deleted\n";
   } else {
      warn $!;
   }
}

0
 
jpiskorCommented:
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;

0
 
FishMongerCommented:
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now