Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting some but not all sub directories using Chef

I have several sub-directories inside a parent directory that I need to remove using my cookbook, but there are many other sub-directories I need to keep. Is there a programmatic way I can achieve the delete of certain sub-directories or do I have to manually specify each one that I wish to remove like so;

directory "/var/lib/foo" do
  action :delete
end
like image 306
Ste-3PO Avatar asked Dec 13 '25 22:12

Ste-3PO


1 Answers

You could just specify a desired pattern with a regular expression and delete all (sub)directories matching that pattern.

Something like that:

Dir["/main/{sub1,sub2,sub3}"].each do |dir|
  file ::File.expand_path(dir) do
    action :delete
  end
end

For more information on how to match the desired files using Dir see the Ruby docs on Dir.

This related SO question might also be useful.

like image 183
Kostas Rousis Avatar answered Dec 15 '25 12:12

Kostas Rousis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!