Challenge

Problem

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.

Difficulty: Beginner

Instructions

  1. Accept command-line arguments: directory path, file pattern (e.g., '*.log'), and age threshold in days
  2. Find all files matching the pattern in the directory
  3. Compare each file's modification time against the threshold
  4. Delete files older than the threshold
  5. Output format: 'Deleted: filename.log (45 days old)' for each deleted file
  6. Output summary: 'Total: X files deleted, Y.YY MB freed'
  7. Sort deleted files by name alphabetically

Files

Editable
Read-only

Hints

Hint 1

ARGV is an array containing command-line arguments

Hint 2

Convert days to seconds: days * 86400 (seconds in a day)

Hint 3

Time.now - seconds gives you a Time object for comparison

Hint 4

File.mtime returns the modification time as a Time object

Hint 5

File.size returns size in bytes, divide by 1048576.0 to get MB

Hint 6

Use sprintf('%.2f', number) to format decimals to 2 places

Provided Files (Read-only)

1. Delete logs older than 30 days

Input:
delete_old_files('logs', '*.log', 30)
Expected Output:
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

2. Delete all old logs

Input:
delete_old_files('logs', '*.log', 30)
Expected Output:
Deleted: access_old.log (90 days old)
Deleted: error_old.log (90 days old)
Total: 2 files deleted, 0.00 MB freed

3. No files to delete

Input:
delete_old_files('logs', '*.log', 30)
Expected Output:
Total: 0 files deleted, 0.00 MB freed
+ 2 hidden test cases