Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to help change file user ID's UNIX
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";
}
    •    
  • Currently 4.00 / 5
  You rated: 2 / 5 (4 votes cast)
 
[29,362 views]  

A script to help change file user ID's | 10 comments | Create New Account
Click here to return to the 'A script to help change file user ID's' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
or try this
Authored by: soellman on Oct 22, '01 03:11:33PM

sudo find <directory> -user <olduidnumber> -exec chown <newuid> {} \; -print

for instance:

sudo find / -user 500 -exec chown oliver {} \; -print


this should do the same thing, at about the same speed.

NOTE: this board is erasing the backslash directly before the semicolon.. insert it after you paste into the terminal.

cheers,
-o



[ Reply to This | # ]
or try this
Authored by: soellman on Oct 31, '01 03:36:48PM

warnings!

yes, this is only one piece of the puzzle if you're trying to change user ids. you'll also want to go into the netinfo manager and change the groups referring to userid 501, in addition to the user whose id is changing from 501 to whatever.

I would recommend doing any of this while logged in as root. You may also want to deal with (delete) the .Trash/501 folders (something like that, basically a uid specific trash).



[ Reply to This | # ]
or try this
Authored by: smurf on Nov 02, '01 03:31:33AM

Still too much work.

sudo find / -xdev -user 501 -print0 | xargs -0 chown 202

(501 was my old uid; 202 is the new one)

The -xdev prevents walking off onto your NFS-mounted volumes.
The -print0 and -0 force zero-byte filename termination instead of newlines, which is important because otherwise xargs will interpret spaces too. :-(



[ Reply to This | # ]
or try this
Authored by: Xzklm on Aug 02, '04 02:37:19PM

well this threat is quite old, but i had to deal with this topic lately
the command line work well, if you add anouther sudo, like:

sudo find / -xdev -user 501 -print0 | sudo xargs -0 chown 202
.........................................................^^^



[ Reply to This | # ]
or try this
Authored by: DMCrimson on Jun 15, '05 05:40:22PM

I decided to try this one when I changed my uid & guid from 503 to 501 (a bit hosed installation, ended deleting the first account). Due to my own stupidity, I ended up with total hang and having to reboot the machine, resulting with no rights whatsoever to my own homedir. luckily, I was able to use the other account to find the information mentioned above, thus regaining the system:)



[ Reply to This | # ]
Warnings!!
Authored by: EatingPie on Oct 23, '01 07:08:42PM
I pretty-much did the same thing in OSX 10.0.4 a few weeks before upgrading to 10.1, and it caused all kinds of problems. Note that I did so to my primary "Admin" user (I only have one user on my machine, me).


Note: The Admin user (typically, the first user you create) has a userID of 501 when you set up your computer.


First off, some Applications would install, but not be runnable. This is becuase the user ID in the subfolders of the .app bundle, for some reason, were installed wtih the Apple-assigned 501 of Admin. And since I was no longer Mr. 501, I couldn't access a bunch of stuff in that bundle. The Moose's Apprentice was one application I recall being particularly unhappy.



Second, and far worse, was my installation of MacOS X 10.1. It pretty-much failed miserably (the misery was mine, actually). I had a system that seemed half-and-half 10.0/10.1. The Preferences Pane, for example, would not come up at all. I ended up reformatting my drive and re-installing a basic OSX 10.0.4 and slapping 10.1 on top of that.



Now, I made one other change, I moved my Applications Folder, and created a symbolic link to it, named "Applications" so installers could still find it. This worked fine, but it may have been the actual cause of my hosed-up 10.1 install. I just don't know for sure. But until someone actually tests this out---with lots of time and hard-drives to spare---it is worth a note of warning that changing your userID may screw you up your next upgrade!



Finally, if you do decide to change your userID, you should heed one other warning:


Do not log out until you change your Home directory, and all sub-directories, to the new userID!


If you log out before changing your Home directory, you will not be able to log back in! This is just like the hosed-up-Application problem. You're not Mr. 501 any more, so you cannot access anything that has a userID of 501... Including your preferences, which the Finder seems to rely on.



That said, I would like to hear if people do have successes with changing their userID. I would like to again, but after the hassles I had before, I 've chosen to contently reamain Mr. 501 for the time being.



-Pie



[ Reply to This | # ]

Verified: Moving Applications Folder Hosed 10.0.4 to 10.1 Upgrade
Authored by: EatingPie on Oct 24, '01 03:58:43PM

Apologies. Just found another user on MacFixit that moved their Applications Folder, and they had the exact same 10.0.4 --> 10.1 upgrade problem that I described. So it looks like the userID change is NOT the cause of the upgrade issue.

Other Warnings still apply...



[ Reply to This | # ]
what's the fuss?
Authored by: adrew on Feb 15, '02 10:33:59AM

I simply added a new group to using Netinfo Manager on MacOSX with the same gid as the nfs mounted directory, then changed the permissions of the mounted directory on my nfs server accordingly (in this case dr-wr-w---). Or am I missing the point?



[ Reply to This | # ]
A script to help change file user ID's
Authored by: jms1 on Nov 13, '03 12:36:28AM

If you also have to change a group-id to match what's on an NFS server, remember to "chgrp" your files as well.

example: old uid/gid=501, new uid/gid=301

find / -user 501 -exec chown 301 {} \;
find / -group 501 -exec chgrp 301 {} \;



[ Reply to This | # ]
A script to help change file user ID's
Authored by: scstraus on Aug 11, '05 07:48:49AM

Make sure you open a new shell after you change your owner or you'll get these errors:

Xanadu:~ scstraus$ sudo find ./ -user 501 -exec chown scstraus {} ; -print
sudo: uid 501 does not exist in the passwd file!

Xanadu:/usr/local/scripts scstraus$ sudo ./fixowner 501 scstraus /
sudo: uid 501 does not exist in the passwd file!

---
I came into this game for the action, the excitement. Go anywhere, travel light, get in, get out, wherever there's trouble, a man alone.



[ Reply to This | # ]