use File::Find::Node

I have to use the perl module File::Find::Node in a script which have to do the code in attach

find /files  -type f -mtime +10 | grep -v "/storico/" | grep -v "error" | grep -v "body_email" | grep -v "/in/" | grep -v "/out/" | grep -v "/appoggio/" | grep -v "/responsi_doppi/" | grep -v .sh$| grep -v .pl$

Open in new window

Who is Participating?
Todd MummertConnect With a Mentor Commented:

first the grep on the end is illegal, since grep has to take an argument...    I don't know what you want there, so I'll ignore it in the script below.

Also, why is using File::Find::Node a requirement?     Well, in any case, it works like this:

#!/usr/bin/perl -w
use strict;
use File::Find::Node;
my $dir = '/files';     # path for find
my $mtime = 10;         # find file before N days ago
my $now = time;
# this isn't strictly correct, since find removes fractional days
my $cutoff = $now-$mtime*24*60*60;
my $f = File::Find::Node->new($dir);
sub proc {
    my $f = shift;
    return if $f->type ne "f";
    return if $f->path =~ m,/storico/|error,;
    return unless $f->mtime < $cutoff;
    print $f->path."\n";

Open in new window


use File::Find::Node;
sub found {
    my $node = shift;
    print $node->path . "\n";
my $f = File::Find::Node->new('/files');
  sub {
  grep {
        -f $_ 
    and -M _ > 10
    and !m|/storico/|
    and !/error/
    and !/body_email/
    and !m|/in/|
    and !m|/out/|
    and !m|/appoggio/|
    and !m|/responsi_dopi/|
    and !/\.sh$/
    and !/\.pl$/}

Open in new window

Todd MummertCommented:
sorry..that was my browser not giving me scroll bars

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

blattamaxAuthor Commented:
this is the find command
find /files  -type f -mtime +10 | grep -v "/storico/" | grep -v "error" | grep -v "body_email" | grep -v "/in/" | grep -v "/out/" | grep -v "/appoggio/" | grep -v "/responsi_doppi/" | grep -v .sh$| grep -v .pl$
The code I posted handles the find and all of the grep parts.  It will run faster than the code without the filter (which filters the results), because directories that are filtered out will not be searched.  The results are printed to the screen.  If you'd like the results somewhere else, let me know.
blattamaxAuthor Commented:
thank u Adam , if i'd have to remove the results?
>> if i'd have to remove the results?
Meaning, you want to delete the files?  If so, you can update the found subroutine to delete the files.
sub found {
    my $node = shift;
    my $p=$node->path;
    print "$p\n";   #Remove this line if you don't want to print the names of files
    unlink($p) or warn "Could not remove '$p': $!\n";

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.