Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

search folder and delete certain files with perl

by ravvles (Initiate)
on Dec 07, 2011 at 19:35 UTC ( #942316=perlquestion: print w/ replies, xml ) Need Help??
ravvles has asked for the wisdom of the Perl Monks concerning the following question:

Guys,

I wonder if anyone can help me. I'm fairly new to Perl and I'm due to go on my first Perl for beginners course next week.

I've been set a task my boss to do the following ... but i'm really struggling to do it ( kinda unfair as I don't know Perl AT ALL yet :-( ) ....

Use a Perl script to .......

Search the contents of a specific folder ( /var/opt/xim/syslogs/ ) on a linux computer. Delete any files in that folder that begin with "Syslogd" (eg Syslogd001 Syslogd002 Syslogd003 Syslogd004 etc) and are older than 12 hours ( or haven't been modified in the past 12 hours )

Many Thanks

Rav's

Comment on search folder and delete certain files with perl
Re: search folder and delete certain files with perl
by CountZero (Bishop) on Dec 07, 2011 at 19:42 UTC
    One of the major benefits of Perl is CPAN, which allows us to not re-invent the wheel. So have a look at File::Find::Rule. It will do all you need.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: search folder and delete certain files with perl
by TJPride (Pilgrim) on Dec 07, 2011 at 21:01 UTC
    Why bother with a module when stat is available?

    use strict; use warnings; my ($path, $cutoff, $handle, $file, $c); $path = '/var/opt/xim/syslogs/'; $cutoff = time() - 3600*12; ### 12 hours ago $c = 0; ### Fail if you don't have read permimssions opendir ($handle, $path) || die; while ($file = readdir($handle)) { ### File name must start with pattern next if $file !~ /^Syslogd/; ### Check modified time in seconds since epoch next if (stat $file)[9] > $cutoff; ### Error if you don't have write permissions if (unlink "$path$file") { print "$file removed.\n"; $c++; } else { print "$file could not be removed.\n"; } } print "$c total files removed.\n";
      Because it is so much more compact:
      use Modern::Perl; use File::Find::Rule; my $directory = '/var/opt/xim/syslogs/'; my $age = time - 12*60*60; unlink for File::Find::Rule->file() ->name('Syslogd*') ->mtime("<$age") ->maxdepth( 1 ) ->in($directory);

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Maybe slightly, I could have compacted my example quite a bit. But I personally prefer doing at least simple things on my own rather than relying on a black box that may or may not work as intended. Maybe this is an extremely reliable module that everyone uses, but how do I know?
Re: search folder and delete certain files with perl
by Eliya (Vicar) on Dec 07, 2011 at 21:52 UTC

    Maybe just use the system tool?

    $ find /var/opt/xim/syslogs -maxdepth 1 -type f -name "Syslogd*" -mtim +e +0.5 -delete

    (Run without -delete first, to make sure you picked the right files!!)

      Yup... what came to my mind right away was find and maybe also xargs.   (Both in a Unix/Linux environment.)

Re: search folder and delete certain files with perl
by jwkrahn (Monsignor) on Dec 07, 2011 at 22:23 UTC
    #!/usr/bin/perl use warnings; use strict; for my $file ( </var/opt/xim/syslogs/Syslogd*> ) { if ( -M $file > .5 ) { unlink $file or warn "Cannot delete '$file' because: $!"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://942316]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-09-01 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (6 votes), past polls