Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: removing files with perl based on age and name

by graff (Chancellor)
on Jul 18, 2014 at 23:20 UTC ( [id://1094273]=note: print w/replies, xml ) Need Help??


in reply to Re^3: removing files with perl based on age and name
in thread removing files with perl based on age and name

The "stat" function is implemented in Perl as a direct system call OS library call because it is commonly recognized that starting up and shutting down a whole shell process just to stat a file will tend to be a noticeable waste of run time, especially given the very typical usage (like in the code above) that could end up doing this relatively costly business many many times. (update: likewise for unlink, rename, and similar built-ins)

Plus, /usr/bin/stat tends to be one of those "core utilities" that don't actually have equivalent command-line syntax from one unix/linux OS to the next. (update: it might be /usr/local/bin/stat, or as pointed out above, it might not be found at all on some systems)

To clarify the run-time issue... Just for grins I found a directory on my mac laptop (2.2GHz intel core i7, osx 10.8.5, perl 5.12) that happens to contain 12.6K files, and I tried two different scripts:

$ cat /tmp/j1.pl #!/usr/bin/perl use strict; use warnings; while (<>) { chomp; my $stat = `stat $_`; } $ cat /tmp/j2.pl #!/usr/bin/perl use strict; use warnings; while (<>) { chomp; my $stat = stat $_; } $ cd path/with/12.6K_files/ $ ls | time /tmp/j1.pl 60.05 real 12.76 user 31.45 sys $ ls | time /tmp/j2.pl 0.07 real 0.01 user 0.02 sys
I did it a few times, in both orders (j1 right after j2 and vice-versa), and the results were consistent. Imagine what the difference would be over a whole disk with, say, a million files.

As for the cross-platform portability issue… I have to ask: which OS / version of stat are you using? The version of /usr/bin/stat I have on my mac doesn't support the particular command-line syntax you used, so even if I wanted to run your script, I couldn't.

UPDATE: Oh, I forgot to mention… Have you tried running your script in a directory where file names contain spaces, and/or parens, and/or apostrophes, and/or ampersands, and/or … (I hope you get the idea).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1094273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found