You are doing this very inefficiently. File::Find goes to the trouble to chdir into the directory that holds the files you are looking at and then you do operations on a long path string1 which means the operating system needs to reparse the path and pull information about all of the parent directories out of (and perhaps in to) the cache.

First, set $File::Find::dont_use_nlink= 1; (to make your script portable -- it won't be using this on Win32 since Win32 file systems just don't work that way), then use -M _ instead of your current -M $file, unlink($_) instead of unlink($file), and (much less importantly) use $_ =~ m|\.pdf$| (and you can drop the $_ =~ part if you so desire).

Note that I said -M _ and not -M $_, the former being a bit more efficient because $File::Find::dont_use_nlink= 1; assures that File::Find has already done an lstat on $_ for you so you don't need to do it again.

                - tye

1 ...which might even be incorrect if $ARGV[0] didn't contain an absolute path name. I'd have to reread the File::Find docs to determine that, but what you do to fix this is the same regardless so I won't.

(updated: trivial)


In reply to Re: Some File::Find woes. ($_ not $name) by tye
in thread Some File::Find woes. by ibanix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.