Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 23 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 '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 '14 at 0:38
    
In case anyone else is looking for how to find files not owned by a certain user (user's files in a home directory that are not owned by that user), you can add '!' to the -user other-user section to find the opposite. For example, find . ! -user other-user. – Jon May 13 at 16:48
    
Often the above is more useful when you add -group old-group. That way you can preserve group ownership (admin, staff, sillyothergroupname) – brianlmerritt Jul 12 at 7:55

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

The -h option of chown changes the permission of the symbolic link files themselves and not just the target.

Which could be crucial when doing this on software binary folders, which use symoblic links to switch between versions.

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

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .