Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks , I am trying to read a directory and and delete files that are 30 days older excpet for couple of directories . I am doing the following :
system(qq(find /dir/dir../ -mtime +30 -exec egrep -v 'OLD|NEW' -exec r +m -rf {} \;));
it is not working , I am not sure if the syentax is wrong or the way i am doing it in the system call is wrong. It doesn't give an error , and don't delete the files. Can someone advice? thanks

Replies are listed 'Best First'.
Re: command using system
by Zaxo (Archbishop) on Feb 02, 2004 at 21:36 UTC

    No need to go to the system,

    while (</path/to/*>) { unlink $_ if -f and -M >= 30; }
    The -f test is important, you don't want to unlink a directory.

    After Compline,
    Zaxo

      what if I am deleting directories as well ?

        rmdir, after emptying.

        After Compline,
        Zaxo

        I suspect you want a second pass to remove empty directories (or after the unlink, check if that was the last file in the directory and unlink it). I very much doubt you want to remove directories with a modification time < 30 days ago, since they could very well have files with more recent modification dates.
Re: command using system
by virtualsue (Vicar) on Feb 02, 2004 at 23:51 UTC

    I think your specific problem here is due to the fact that you used interpolating quotes (qq()) and this is messing up your find command because the \ is being swallowed up. Use \\ to get a literal \ in an interpolated string.

Re: command using system
by Fletch (Bishop) on Feb 02, 2004 at 21:40 UTC

    See also perldoc find2perl and perldoc File::Find.