Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do you write a perl script to calculate the age of a file

by kleinbiker7 (Sexton)
on Jun 19, 2001 at 03:48 UTC ( #89522=perlquestion: print w/ replies, xml ) Need Help??
kleinbiker7 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to write a script that will delete a file if it is more than 2 days old. Does Perl have the means to do this job?

Thanks.

Comment on How do you write a perl script to calculate the age of a file
Re: How do you write a perl script to calculate the age of a file
by ezekiel (Pilgrim) on Jun 19, 2001 at 03:58 UTC
    I think the -M operator gives you the number of days since a file was last modified. Hence:
    if (-M $filename > 2) { unlink ($filename); }
    should work if you want to delete files that were last modified over 2 days ago. -A and -C do the same thing based on last access time and last inode change.
      Ah, when will the golf urge pass? You could do: unlink $filename if -M $filename > 2;
      saving yourself two parentheses and two squiggly braces. ;-)

      It even reads exactly as the questioner proposed: "delete the file if it is more than two days old".

Re: How do you write a perl script to calculate the age of a file
by marcink (Monk) on Jun 19, 2001 at 03:59 UTC
Re: How do you write a perl script to calculate the age of a file
by kurt1992 (Novice) on Jun 19, 2001 at 07:49 UTC
    Perl has the means to do many things, though this particular example can also be solved with the following shell command, or bourne shell script containing this command. Be careful where you invoke this!
    # find . -type f -mtime +2 | xargs -i rm {}
    In Perl, you could use the File::Find module in conjunction with the aforementioned -M operator to accomplish the same thing as that shell command, and much more. You should also check out the stat function, which provides detailed inode information. Again, please be careful. This code deletes everything under $dir.
    #!/usr/bin/perl -w use strict; use File::Find; my @files = (); my @paths = (); my $dir = '/home/kurtw/perlmonks/test5'; find(\&_filewanted, $dir); for my $file ( @files ) { if (unlink $file) { print "removing $file\n"; } else { print "could not unlink $file\n"; } } sub _filewanted { if ( ((-M $_) > 2) && -f $_ ) { # files, not directories push @files, $File::Find::name; } }
Re: How do you write a perl script to calculate the age of a file
by petdance (Parson) on Jun 19, 2001 at 07:52 UTC
    You can also look up the stat() function if you want the absolute date something was created.

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-07-11 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls