Ubuntu – chown subfolders with current folder’s owner and group

chowncommand linepermissions

In a bash script I want to recursively chown all subfolders of an arbitrary folder with the user and group owners of that given folder.

My approach is something like using stat -c "%U %G" .

But that returns only a pattern of username[whitespace]groupname

Of course I could just try different tools to replace the whitespace with a : but I would prefer to use a more "built-in" way if there is one, to just apply current ownership to the sub folders.

Best Answer

chown, like chmod, can use a reference:

chown [OPTION]... --reference=RFILE FILE...

Change the owner and/or group of each FILE to OWNER and/or GROUP.  With
--reference, change the owner and group of each FILE to those of RFILE.

So:

chown --reference=/some/folder -R /some/folder