Re: Modifiaction time of file



anu p wrote:
Hi,

Hello,

I have a requirement where I need to look at the age
of file, if it's older than 2 days, I should remove
it.
I am trying to use the '-M ' filehandle in my script,
it does not work.

I get the following error
Use of uninitialized value in numeric gt (>) at
./temp_age.pl line 12.


#!/usr/bin/perl

use strict;
use warnings;

my $fail_dir =
"/home/anupamaperi/failed_tests/regress_26";
my ($file, $age);

opendir (DIR, $fail_dir) || die "cannot open, no
$fail_dir dir $!";
while ($file = readdir (DIR)) {
if (-M $file > 2) {
print("$file\n");
unlink($file);
}
}

Any ideas where am I going wrong?

perldoc -f readdir

readdir DIRHANDLE
Returns the next directory entry for a directory opened by
"opendir". If used in list context, returns all the rest of
the entries in the directory. If there are no more entries,
returns an undefined value in scalar context or a null list in
list context.

If you’re planning to filetest the return values out of a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"readdir", you’d better prepend the directory in question.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Otherwise, because we didn’t "chdir" there, it would have been
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testing the wrong file.
^^^^^^^^^^^^^^^^^^^^^^^

opendir(DIR, $some_dir) ││ die "can’t opendir $some_dir: $!";
@dots = grep { /^\./ && ‐f "$some_dir/$_" } readdir(DIR);
^^^^^^^^^^^^^^^^^
closedir DIR;



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: readdir behavior?
    ... If used in list context, returns all the rest of the entries in the ... By the time you call the 2nd readdir, there are no more entries and so ... print join "\n", readdir $dh; ...
    (perl.beginners)
  • Re: nfs client readdir caching issue?
    ... duplicate directory entries. ... a first cookie value of 13. ... In the other capture, the responses ... Subsequently, the readdir cache needs ...
    (Linux-Kernel)
  • Re: JFS2 fundamentally broken - exploit code
    ... In the first shell, start makedirentries and hit enter a few times to ... readdir() because (as the man ... the result of adding entries during the readdiris not ... start the testreaddir program and hit enter a few ...
    (comp.unix.aix)
  • [RFC][PATCH 9/15] Simple union-mount readdir
    ... This is a very simple union mount readdir implementation. ... The directory entries are read starting from the top layer and they ...
    (Linux-Kernel)
  • In what order does readdir() see a directory?
    ... In what order does readdir() see the ... as a sort of public service -- a search ... those two pesky entries right away with two ...
    (comp.lang.perl.misc)