A quicker way to change owner/group recursively?
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 .
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).