You're already putting in the effort to change user IDs. Why stop there? If it doesn't matter to the search what the current working directory is, just take one more step and change the current working directory in the code before the find. That way it'll try to change back to that directory instead of the one from which the program was run.

When the program exits, any path changes will be lost anyway. Your prompt will still come back to your shell's current working directory since environment changes do not flow upstream.

I recommend using chdir to change to the root directory for sake of portability, but you could make it any other directory the account can access.

Below is some example code. I can run this as root in root's home directory without any errors. It changes to my non-root account, changes to the root ('/', not to be confused with '/root') directory, searches a directory my non--root account can access (and actually owns in this case, but that's not important), and File::Find returns to '/' when it's done.

use File::Find; $< = $> = 500; sub w { print $_ . "\n" } chdir q(/); find( \&w, qw( /home/chris/Pictures ) );

There's no reason for File::Find not to return to the directory from which it started. There's also no reason to have it return to a directory where the user account under which it is operating has no privileges. No code that might be executed afterward would have rights to that directory either anyway.


In reply to Re: Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list? by mr_mischief
in thread Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list? by puterboy

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.