Deleting multiple files in a directory using chef ruby

2.2k views Asked by At

I have been looking for a chef ruby code to delete multiple files in a directory matching on a file name string

Lets say if my D drive contains the file names - eula1.txt, eula2.txt, res1.dll, res2.dll, pvn1.txt, pvn2.txt

In the above example, i would need a chef ruby code which i can include in the cookbook to delete all occurrences of a file starting with eula* and res*, i.e it should delete 4 files in the directory

My Final output should contain only 2 files - pvn1.txt,pvn2.txt in the directory.

2

There are 2 answers

0
Pavan On

I could use 'FileUtils' to delete the files with the matching condition. Since FileUtils is pure ruby command, i have to embed this code inside a ruby_block in my chef cookbook.

The below statement worked for me

ruby_block "Deleting the eula*,install* files...." do    
  block do    
    FileUtils.rm Dir["path/to/folder/eula*","path/to/folder/install*"]    
  end
end
0
coderanger On

Your best bet is to use an execute resource like:

execute 'del eula* res*'

While it is possible to do this with direct file deletion, it is much more complex and probably out of scope for you.