in reply to del files that is 7 days or older

To be real simple, instead of using the month/date/year approach, you could simply use epoch as your time refernce, and do some simple math to get your answer:
#!/usr/bin/perl opendir(DIR, "/home/archive/logs/old"); @FileList = readdir(DIR); closedir DIR; $current_epoch = timelocal ((localtime)[0,1,2,3,4,5]); foreach $file (@FileList) { $file .= "/home/archive/logs/old" . $file; $file_epoch = (stat($file))[9]; if (($current_epoch - $file_epoch) > "604800") { unlink("$file); } }
Or you could look at Date::Calc to do it your way.

Update: merlyn, I noticed what I had typed and was changing as you were correcting me. Heh, I gotta be faster with the code checking next time.

Replies are listed 'Best First'.
Re: Re: del files that is 7 days or older
by merlyn (Sage) on Feb 15, 2001 at 00:55 UTC
    Ouch. No. Please, not:
    @FileList = `ls -1`;
    • The -1 isn't needed if the output is not a terminal.
    • This fails on filenames that contain newlines
    • This fails on non-Unix systems.
    • This fails if ls is not what you think it is.
    • This doesn't list the files that begin with dot, unless you're root.
    • You didn't chomp the list, so the names still end in newline.
    Much simpler:
    @FileList = <*>;
    Or, to get files that begin with dot:
    @FileList = <.* *>;

    -- Randal L. Schwartz, Perl hacker