Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recursively change the owner and group on a directory with Chef?

Tags:

the resource_directory has only 2 actions available: create and delete

I need to update the owner and group of a directory recursively.

How do I do that?

using a simple resource_execute?

execute "chown-data-www" do
  command "chown -R www-data:www-data /var/www/myfoler"
  user "root"
  action :run
end
like image 906
zabumba Avatar asked May 28 '14 20:05

zabumba


People also ask

How do I change the ownership of a directory in recursively?

The easiest way to use the chown recursive command is to execute “chown” with the “-R” option for recursive and specify the new owner and the folders that you want to change.

How do I change directory recursively in Linux?

You can change permissions of files using numeric or symbolic mode with the chmod command. Use the chmod command with the R (recursive) option to work on all directories and files under a given directory. The permissions of a file can be changed only with the user with sudo priviledges, or the file owner.

How do you recursively change chmod?

It is common to use the basic chmod command to change the permission of a single file. However, you may need to modify the permission recursively for all files within a directory. In such cases, the chmod recursive option ( -R or --recursive ) sets the permission for a directory (and the files it contains).

Does chmod work on subdirectories?

Changing permissions with chmod To modify the permission flags on existing files and directories, use the chmod command ("change mode"). It can be used for individual files or it can be run recursively with the -R option to change permissions for all of the subdirectories and files within a directory.


1 Answers

You can set the default action to nothing then have resources that may screw things up notify the perm fixer:

execute "chown-data-www" do
  command "chown -R www-data:www-data /var/www/myfoler"
  user "root"
  action :nothing
end

resource "that may screw up perms" do
  stuff "one two three"
  notifies :run, execute "chown-data-www"
end

with more options you could have the action :run but not if the parent folder is already the correct perms. You could alter this to include a deeper/problem file/directory, or with a find command similar to this

execute "chown-data-www" do
  command "chown -R www-data:www-data /var/www/myfoler"
  user "root"
  action :run
  not_if '[ $(stat -c %U /var/www/myfolder) = "www-data" ]'
end

Edit: fix to reflect comment below

like image 188
Bill Warner Avatar answered Sep 28 '22 09:09

Bill Warner