13

This question already has an answer here:

Currently, when I want to change owner/group recursively, I do this:

find . -type f -exec chown <owner>.<group> {} \;
find . -type d -exec chown <owner>.<group> {} \;

But that can take several minutes for each command. I heard that there was a way to do this so that it changes all the files at once (much faster), instead of one at a time, but I can't seem to find the info. Can that be done?

marked as duplicate by Stephen Kitt, jimmij, MelBurslan, garethTheRed, slm linux May 26 '16 at 22:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

20

Use chown's recursive option:

chown -R owner:group * .[^.]*

Specifying both * and .[^.]* will match all the files and directories that find would. The recommended separator nowadays is : instead of .. (As pointed out by justins, using .* is unsafe since it can be expanded to include . and .., resulting in chown changing the ownership of the parent directory and all its subdirectories.)

If you want to change the current directory's ownership too, this can be simplified to

chown -R owner:group .
3

For commands like chown that have their own recursion it is fastest to use that option:

 chown -R owner:group * .[^.]*

Warning! In some shells, the form chown -R owner:group * .* replaces owner in root directory / . Because .* means ../../../../root, ../bin ... etc. All paths. However, the most widely used shell, bash, doesn't apply . and .., expanding patterns.

However it is useful to know that the main problem that slows down your use of find is that you invoke chmown on every single directory and file found. It is much quicker to use:

find . -type f -exec chown <owner>:<group> {} +
find . -type d -exec chown <owner>:<group> {} +

as each time chown is called with as many parameters as fit on the commandline.

That change works for other commands, that don't have a built-in recursion option like chown, as well. And it works (and improves speed) in situations where there is such a recursion option but you cannot use it (e.g. when using chmod, and you only want to change directories).

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