Recursively chown all files that are owned by a specific user

34

15

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?

Michael Waterfall

Posted 2013-09-20T18:16:03.640

Reputation: 587

Answers

62

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

StephenTrapped

Posted 2013-09-20T18:16:03.640

Reputation: 766

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 – 2016-05-13T16:48:32.617

Often the above is more useful when you add -group old-group. That way you can preserve group ownership (admin, staff, sillyothergroupname) – brianlmerritt – 2016-07-12T07:55:40.143

ps - chown may require sudo – brianlmerritt – 2016-07-12T08:33:03.157

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 – 2014-02-14T18:09:17.400

2I'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 – 2014-06-10T00:38:59.430

13

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.

Rik

Posted 2013-09-20T18:16:03.640

Reputation: 11 800

chown --from=CURRENT_OWNER:CURRENT_GROUP -R new_owner:new_group * will skip hidden files in the folder command is ran, however chown --from=CURRENT_OWNER:CURRENT_GROUP -R new_owner:new_group . works for the hidden files in the parent folder too. – Lashae – 2019-07-23T09:40:25.883

+1 but that won't work on OSX, its chown doesn't have that option, see here.

– terdon – 2013-09-20T18:40:25.243

Oops, indeed, i missed the tag osx :) Voted @StephenTrapped up. – Rik – 2013-09-20T18:45:00.233

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

Tarun

Posted 2013-09-20T18:16:03.640

Reputation: 131