Application logs accumulate relentlessly, consuming disk space and eventually crashing servers. This drill teaches you to build a maintenance script that identifies and deletes files older than a specified number of days. You'll learn time arithmetic, file metadata handling, and summary reportingโessential skills for system administration tasks.
Read the metadata.json file using File.read and parse with JSON.parse
Iterate through the 'files' array in the metadata
Compare each file's age_days against the threshold
Use File.delete(path) to remove files
Track total bytes freed and convert to MB by dividing by 1048576.0
Use sprintf('%.2f', number) to format MB to 2 decimal places
delete_old_files('logs', 30)
Deleted: app_old.log (45 days old) Deleted: error_old.log (60 days old) Total: 2 files deleted, 0.00 MB freed
delete_old_files('logs', 30)
puts '---'
puts File.exist?('logs/access_recent.log')
puts File.exist?('logs/debug_recent.log')
Deleted: app_old.log (45 days old) Deleted: error_old.log (60 days old) Total: 2 files deleted, 0.00 MB freed --- true true
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept