Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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 :)

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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
share|improve this answer
    
what's the significance of $ in bash? –  ivan Oct 4 '12 at 15:27
    
It's how you refer to variables you have assigned. By setting dir='/Applications', for instance, any future use of $dir will be effectively replaced with '/Applications'. Also, in the example of the $() section, it's a way to make bash act as if the output of a particular command or commands were typed in-place. For instance, man $(echo 'grep') is equivalent to man grep, because echo 'grep' returns grep. –  Iain Dawson Oct 4 '12 at 15:48
1  
No need for stat, just use find directly: find $dir -user $olduser -exec chown $newuser '{}' \; –  patrix Oct 4 '12 at 18:09
    
Ooh. I didn't know find could be filtered like that. Neat. –  Iain Dawson Oct 4 '12 at 21:09
    
@patrix- I think I understand the above code, except for the \ . If I understand it correctly, it finds files in $dir that meet two conditions: 1) owned by $olduser and 2) return a zero value exit status when chown $newuser is run on them. I'm guessing it starts with the first condition, and if that condition is met, it then runs chown $newuser on the file to evaluate the second condition. So if the first condition fails (the file's not owned by $olduser), find moves on without chowning that file. A clever way of piping. Is that sort of correct? And thanks for the help! –  ivan Oct 6 '12 at 1:34
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.