简体   繁体   English

使用Chef ruby​​删除目录中的多个文件

[英]Deleting multiple files in a directory using chef ruby

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 可以说我的D盘是否包含文件名-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*, ie it should delete 4 files in the directory 在上面的示例中,我需要一个厨师红宝石代码,可以将其包括在食谱中,以删除所有以eula *和res *开头的文件,即应删除目录中的4个文件

My Final output should contain only 2 files - pvn1.txt,pvn2.txt in the directory. 我的最终输出应仅包含2个文件-目录中的pvn1.txt,pvn2.txt。

Your best bet is to use an execute resource like: 最好的选择是使用execute资源,例如:

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. 虽然可以通过直接删除文件来执行此操作,但是它要复杂得多,可能对您而言范围不大。

I could use 'FileUtils' to delete the files with the matching condition. 我可以使用'FileUtils'删除具有匹配条件的文件。 Since FileUtils is pure ruby command, i have to embed this code inside a ruby_block in my chef cookbook. 由于FileUtils是纯ruby命令,因此我必须将此代码嵌入我的厨师食谱中的ruby_block中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM