Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to find and chown all files that a specific user owns? I did a bunch of things as the wrong user and need to give the correct user ownership of the files.

Is there a recursive and conditional way I can chown a bunch of files and directories?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

You can use this portable find command with something like this.

find . -user old-user -exec chown new-user:new-group {} \;

You can expand this to find specific files with -iname options (non POSIX but available on OSX)

find . -iname '*.html' -user www-data -exec chown www-data:www-data {} \;

The . stands for the current folder and below, so you could use a specific path as a base.

find /var/www/ -iname '*.html' -user www-data -exec chown www-data:www-data {} \;
share|improve this answer
    
That's perfect, thanks! –  Michael Waterfall Sep 20 '13 at 19:07
    
What I was looking for. But just to note that the options after the chown new-user:new-user should probably say new-user:new-group –  Henesnarfel Feb 14 at 18:09
    
I'm guessing the curly brackets should be in quotes to properly handle paths with spaces. So find . -user old-user -exec chown new-user:new-group "{}" \; –  Dan Jun 10 at 0:38

You can use

chown --from=CURRENT_OWNER:CURRENT_GROUP -R new_owner:new_group  *

From the manual chown --help:

  --from=CURRENT_OWNER:CURRENT_GROUP
                     change the owner and/or group of each file only if
                     its current owner and/or group match those specified
                     here.  Either may be omitted, in which case a match
                     is not required for the omitted attribute.

edit: This, of course, only works on linux and most UNIces. For OSX (which is based on BSD) see the solution of @StephenTrapped.

share|improve this answer
    
+1 but that won't work on OSX, its chown doesn't have that option, see here. –  terdon Sep 20 '13 at 18:40
    
Oops, indeed, i missed the tag osx :) Voted @StephenTrapped up. –  Rik Sep 20 '13 at 18:45

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.