Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

if owner A change to owner B

by martap (Initiate)
on Dec 19, 2009 at 18:39 UTC ( [id://813543]=perlquestion: print w/replies, xml ) Need Help??

martap has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I need to change the ownership of about 1e6 files from 17 old owners to 17 new owners. the files are in multiple directories. I can run "chown -R" 17 times in the top directory but I thought there must be a more efficient way and perl is probably my best bet. Instead of using chown which will go over the same file 17 times I was looking at a more intelligent solution which inspects each file individually and checks if the owner is one of the 17 old ones and changes the ownership to the new owner. I assume that this would be much quicker as well. My perl knowledge is limited so I was hoping one of you can help me out. Of course I'm not necessarily asking you to write the script for me but maybe just pointing me in the right direction.

Replies are listed 'Best First'.
Re: if owner A change to owner B
by shmem (Chancellor) on Dec 19, 2009 at 23:16 UTC
    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.

Re: if owner A change to owner B
by zwon (Abbot) on Dec 19, 2009 at 18:50 UTC
Re: if owner A change to owner B
by gmargo (Hermit) on Dec 19, 2009 at 18:49 UTC

    No offense, but it seems like a trivial shell script. Who cares if it takes overnight to run?

    But if you insist on learning perl to do it, check out the File::Find module.

      that's the problem, it needs to complete within 30 min as we only get a 30 min window to bring the app down.

        That's about 500 files per second. That doesn't sound too fast to me, but I've never needed to do that sort of bulk change, either. My first suggestion is to get some data about how fast chown or chown runs, then break up the list of 106 files into however many lists it takes to permit each list to finish comfortably within 30 minutes.

        Alternatively, patch the app or the script that starts the app to chown the file before it's accessed.


        Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found