Re: trouble with this code in deleting files older than 60 days



Jim Gibson <jgibson@xxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> In article <1122055593.914958.125580@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
> George Lewycky <gelewyc@xxxxxxxx> wrote:
>
> > My problem is trying to delete all files older than 60 days in the
> > directory /MLIM_data/archive/cmp through a Perl script.

[...]

> There is no need to shell out from Perl to find and delete old files:
>
> #!/usr/local/bin/perl
> #
> use warnings;
> use strict;
> use File::Find;
>
> my $now = time();
> my $then = $now - (60 * 24 * 60 * 60); # 60 days ago
> find(
> sub {
> my $t = (stat)[9]; #mtime
> unlink $_ if $t < $then;
> },
> 'MLIM_data/archive/cmp'
> );

Nothing wrong with it, but Perl has the -M file test which gives the
file age in days. So (untested)

find sub { -M >= 60 and unlink }, 'MLIM_data/archive/cmp';

should suffice.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.



Relevant Pages

  • Re: Arithmetic Expression Evaluation in print
    ... > gives 0.789154519316799, which is incorrect, while ... > I also get the same results from Perl 5.8.7. ... If you want to post a followup via groups.google.com, ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • Re: Unexpected result from grep
    ... I'd worry about whether I should be using the shell at all. ... as big a deal as Perl. ... Most people can puzzle out even a complicated shell script and also ... seem to prefer ksh and it sure seems like a better shell to me. ...
    (comp.unix.tru64)
  • Re: Unexpected result from grep
    ... I'd worry about whether I should be using the shell at all. ... as big a deal as Perl. ... Most people can puzzle out even a complicated shell script and also ... seem to prefer ksh and it sure seems like a better shell to me. ...
    (comp.unix.shell)
  • Re: ActiveState: Cant spawn "cmd.exe"?
    ... >> When testing with ActiveState perl what shell are you running in? ... Normally, perl is built using MinGW in the cmd.exe shell, following the ... ActiveState perl, too) in a bash shell, though I've not personally done ...
    (comp.lang.perl.misc)
  • Re: how to easily determine shell script is running in background from within script
    ... That's not really the shell. ... That's the exit status of commands. ... process group of the terminal is the process' process group, ... There are a number of perl functions in the POSIX ...
    (comp.unix.shell)