A quicker way to change owner/group recursively?

70,286

Solution 1

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 .

Solution 2

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).

Share:
70,286

Related videos on Youtube

danzo
Author by

danzo

Updated on September 18, 2022

Comments

  • danzo
    danzo almost 2 years

    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?