[alt.sources] Recursion without -R

tchrist@convex.COM (Tom Christiansen) (08/15/90)

In article <494@llnl.LLNL.GOV> rjshaw@ramius.llnl.gov writes:
>What are some quick tricks for getting programs like chmod and chown to
>descend into all subdirectories? Programs without a -R option, that is.

Well, here's a quick way to change all files from a set of old uids and
gids to new ones.  In this example, I want to change group uucp to be
1789, group staff to be 666, user kirk to be 1000, and user bill to be
7777.  This code traverses the file system making those changes, omitting
NFS decents.

This is just a fragment of a larger program that does a lot of other 
sanity checks and configuration stuff not included here.

    #!/usr/bin/perl

    $start = '/'; 		# do whole tree

    %nuid = ( 'kirk',	1000,
	      'bill',	7777 );

    %ngid = ( 'staff', 	666,
	      'uucp',  1789  );

    open(FIND, "find $start \\( -fstype nfs -prune \\) -o -ls |");

    while (<FIND>) {
	split;
	$uid = $gid = -1;
	($file, $user, $group) = ($_[11], $_[5], $_[6]);
	if (defined $nuid{$user})  { $uid = $nuid{$user}; }
	if (defined $ngid{$group}) { $gid = $ngid{$group}; }
	if (($uid != -1 || $gid != -1) && !chown($uid, $gid, $file))
	    { warn "$0: couldn't change $file to $uid.$gid: $!\n"; }
    }


--tom
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"