MacOS – Migrating to new user account: must chown old account’s files… I think

macospermission

Is there a way to selectively chown the files/folders owned by a particular user, while leaving other files/folders untouched? I'm in the process of migrating to a fresh user account on my machine. I copied what I need from the old user's home directory and chowned it recursively, as described on an Apple support page. The support page makes no mention of chowning files/folders elsewhere on the machine, but I've found plenty of stuff in other locations that are owned by the old user:

  • /Applications (lots of stuff)
  • /Library (lots of stuff)
  • /private/var/tmp (a small lot)
  • /private/var/vm/app_profile/ (2 files)
  • /usr/local/clamXav/ (1 easily replaceable file)
  • /usr/local/lib/codec/ (4 files/folders)

I'm planning to delete the old user once I'm confident that everything is working, and I'm not sure what would happen if some critical files are owned by a deleted user. The old user was an admin account, if that matters. Oh, and I'm running OSX 10.4.11 on a PPC mac, if that matters.

Any help appreciated 🙂

Best Answer

You can check and re-assign ownership recursively over a directory tree with find, stat and chown.

#!/bin/bash

olduser=<oldusername> # replace this with your old username
newuser=<newusername> # replace this with your new username
dir=<dir> # replace this with the directory you want to run through

find $dir | while read filename
do
  owner=$(stat "$filename" | cut -d ' ' -f 5)
  if [ $owner == $olduser ]
  then
    chown $newuser $filename
  fi
done