I need to change the ownership of about 1e6 files from 17 old owners to 17 new owners. the files are in multiple directories.

Too little information to come up with a effctive way. How are the old owners mapped to the new owners? how are the ownerships distributed inside the tree? Are those files only, or directories? Are there any old ownerships present in the new ownership list, or the other way round? Priorities? Symbolic or hard links? Are files/directories with different uid/gid contained in directories along the tree?

That said

my %uid = ( 123 => 456, ... ); my %gid = ( 500 => 1024, ... ); open my $process_handle, "find $topdir -type f |"; while ( <$process_handle> ) { chop; # or chomp my($uid, $gid) = (stat)[4,5]; chown $uid{$uid}, $gid{$gid}, $_ if $uid{$uid} && $gid{$gid}; }

seems to do the job (for files only, and only if the values of %uid and %gid aren't present in the key list of %uid and %gid, respectively); but 'chown -R directory' is much more efficient in the case that uid/gid aren't nested inside directories. And chown can be shelled out as well.

This is a typical question for TIMTOWTDY - the job can be done in multiple ways: processing sequentially, using perl's builtin chown, forking for various paths and uid/gid combinations etc. pp.

A shell script - well done - might be even faster, it all depends on your detailed requirements.


In reply to Re: if owner A change to owner B by shmem
in thread if owner A change to owner B by martap

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.