Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all file in directory older than x days using Chef

I tried removing the log files with:

log 'remove compressed log files'
::Dir['/var/log/*.gz'].each { |f| ::FileUtils.rm_rf(f) }

My request is to remove them based to mtime.

like image 862
nithin sunny Avatar asked Dec 06 '25 08:12

nithin sunny


1 Answers

This is just plain Ruby code, not the Chef recipe DSL. A more Chef-ish way to do this would be

Dir['/var/log/*.gz'].each do |path|
  file path do
    action :delete
    only_if { ::File.stat(path).ctime < (Time.now - 60*60*24*7) }
  end
end
like image 194
coderanger Avatar answered Dec 07 '25 22:12

coderanger



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!