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. |