30

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?

53

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 {} \;
  • 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
  • 2
    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 Benamy 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 '16 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 '16 at 7:55
  • ps - chown may require sudo – brianlmerritt Jul 12 '16 at 8:33
10

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.

  • +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
3

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 {} \;

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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