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

In -X FILEHANDLE, how to derive the -C of all files in a $dir variable?

by taint (Chaplain)
on Apr 27, 2013 at 01:43 UTC ( #1030903=perlquestion: print w/ replies, xml ) Need Help??
taint has asked for the wisdom of the Perl Monks concerning the following question:

Greetings everyone,
I've been struggling with an attempt to clobber symlinks in a folder, if they are older than N minutes.
After much work with File::Find, I found that I am unable to make it work. SO, I decided to look to perlfunc's -X (-C in this case). which will return the Creation-time, which I can compare to Current-time. Allowing me to clobber anything older than 11 minutes from now.
Problem is, I'm not smart enough to figure out to how list the files Creation-time with perlfunc's -C. Looking at How do I delete... get's me close. But does mtime (modified) == ctime?
I could get close:
# delete $file if it's not been modified for 15 minutes if ( (stat $file)[9] < time() - (3600 / 5) ) { unlink $file; }
Never mind the example above, it doesn't work, it returns:
Use of uninitialized value in numeric lt (<) at line 12 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistak +e. To suppress this warning assign a defined value to your variables.
Would anyone be willing to provide an example, please?

Thank you for all your consideration


#!/usr/bin/perl -Tw
use perl::always;
my $perl_version = "5.12.4";
print $perl_version;

Comment on In -X FILEHANDLE, how to derive the -C of all files in a $dir variable?
Select or Download Code
Re: In -X FILEHANDLE, how to derive the -C of all files in a $dir variable?
by BrowserUk (Pope) on Apr 27, 2013 at 04:42 UTC

    From perlfunc:

    -M Script start time minus file modification time, in days. -A Same for access time. -C Same for inode change time

    So, for 15 minutes you want any matching file where the return from -C is > than 15*60 / 86400:

    while( glob '*' ) { if( -C() > 0.010416 ) { unlink $_; } }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: In -X FILEHANDLE, how to derive the -C of all files in a $dir variable?
by kcott (Abbot) on Apr 27, 2013 at 06:18 UTC

    G'day Chris,

    The reason that your stat version failed was probably due to $file not existing (possibly deleted in an earlier test). The code is fine; although, I would question your use of parentheses — see the operator precedence table in perlop. Also, note that 15mins. is 900secs. but 3600 / 5 == 720 (i.e. 12 mins.): 60 * 15 would be a clearer indication of the value you want.

    I'd recommend that you run some tests (similar to what I show below) which simply indicate what files are to be deleted. When you're happy everything works, then start using unlink. A backup of the directory you're working in would also be a good idea.

    $ perl -Mstrict -Mwarnings -E ' my $file = q{pm_1030903.dummy}; say "(stat \$file)[9] = ", (stat $file)[9]; say "15 mins. ago = ", time - 60 * 15; if ( (stat $file)[9] < time - 60 * 15 ) { say "[stat] Will delete $file"; } say "-C \$file = ", -C $file; say "15 mins. in days = ", 60*15 / (24*60*60); if ((-C $file) > 60*15 / (24*60*60)) { say "[-C] Will delete $file"; } ' (stat $file)[9] = 1367039733 15 mins. ago = 1367041074 [stat] Will delete pm_1030903.dummy -C $file = 0.0151736111111111 15 mins. in days = 0.0104166666666667 [-C] Will delete pm_1030903.dummy

    -- Ken

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2015-07-03 12:49 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (53 votes), past polls