Application logs accumulate relentlessly, consuming disk space and eventually crashing servers. This drill teaches you to build a maintenance script that automatically deletes files older than a specified number of days. You'll learn time arithmetic, file metadata comparison, and command-line argument handlingโessential skills for system administration tasks.
ARGV is an array containing command-line arguments
Convert days to seconds: days * 86400 (seconds in a day)
Time.now - seconds gives you a Time object for comparison
File.mtime returns the modification time as a Time object
File.size returns size in bytes, divide by 1048576.0 to get MB
Use sprintf('%.2f', number) to format decimals to 2 places
delete_old_files('logs', '*.log', 30)
Deleted: app_2023_01_15.log (285 days old) Deleted: app_2023_02_10.log (259 days old) Total: 2 files deleted, 0.00 MB freed
delete_old_files('logs', '*.log', 30)
Deleted: access_old.log (90 days old) Deleted: error_old.log (90 days old) Total: 2 files deleted, 0.00 MB freed
delete_old_files('logs', '*.log', 30)
Total: 0 files deleted, 0.00 MB freed
Console output will appear here...
Are you sure?
You're making great progress