in reply to In -X FILEHANDLE, how to derive the -C of all files in a $dir variable?
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
|
|---|