perl - Creating a script to delete old log files, that allows me to pass directory/age/regex arguments -


i'm trying create perl script delete old log files. 1 of key things want script able allow me pass arguments directory, name of file (such test.log-*), , age of file.

it's been while since i've used perl , i'm not great anyway, i'd appreciate help. i'm not terribly familiar getopt::long module. here's i'm thinking far, , while i'm sure it's not correct, please give me feedback might assist.

i want run script along lines of "script.pl --dir /release/logs --type test.log-* --days 7"

#!/usr/perl  use strict; use warnings; use data::dumper; use getopt::long;  $file; ($dir,$type,$days); getoptions( 'dir'   => \$dir,             'type'  => \$type,             'days'  => \$days);  foreach $file (<$dir/$type>){    if (-m $file < $days) {        print "\n deleting log more '$days' old:".$file;        unlink $file;        # or die "\n failed remove $file";    } }  exit; 

if insist on using perl, file::find & friends. though if you're on *nix box should aware of find(1) tasks common.

try: find /release/logs -name test.log-\* -mtime +7 -delete

if want test out 1st, leave off -delete flag & print list of files have otherwise deleted.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -