A script to help change file user ID's

Oct 21, '01 09:51:20AM

Contributed by: houchin

I am in the process of getting my Mac integrated into our unix environment so I can NFS mount the unix directories. That meant that I had to change my UID on my Mac to match my Unix UID.

Using NetInfo Manager, it's easy enough to change a UID, and it also nicely updates the UID of all of the files in your official home directory. However, I had created files throughout the system as I installed software and such, which meant I had to update the owner of all of those files myself.

To automate this process, I created the following perl script. It takes the old UID, your short username and the directory to fix (allowing you to selectively fix directories, or allowing you to fix removable media at a later time). If you want to fix everything, just give it '/'.

[Editor's note: Read the rest of this article for Scott's script. I have not tried this on my machine.]

This script requires that it be run with root permissions, so use sudo. Just copy the following text into a file and put it wherever you put your own custom scripts (I suggest /usr/local/scripts):


#!/usr/bin/perl

# Make sure this is being run by root. Otherwise, all of the chowns
# are likely to fail
if ($< != 0)
{ print "fixowner must be run with root permissions. Try running\n";
print "it with sudo.\n";
print "\n";
exit -1;
}

if (@ARGV < 3)
{ print "USAGE: fixowner olduid shortname dirtofix\n";
print "where olduid is the old UID of the changed user\n";
print " shortname is the short username of the changed user\n";
print " dirtofix is the directory tree that you want to fix\n";
print "\n";
exit -1;
}

$olduser = $ARGV[0];
$newuser = $ARGV[1];
@newuserpw = getpwnam($newuser);
if (@newuserpw == 0)
{ printf "No such user: $newuser\n";
exit -1;
}

$newuid = $newuserpw[2];
$start = $ARGV[2];

open (LIST, "find \"$start\" -user $olduser \! -path \"/dev/*\" |") ||
die "Can't pipe in find results\n";

while (<LIST>)
{ ($file) = /(.*)\n$/;
chown $newuid, -1, $file;
print "$file\n";
}

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20011021095120946