How to Delete Files Older Than a Specific Number of Days in Ubuntu (find -mtime -delete)

The Automated Purging Requirement

On an Ubuntu Linux server, log files, temporary caches, and session data accumulate relentlessly. If left unchecked, the /var/log/ or /tmp/ directories can consume gigabytes of disk space, eventually crashing critical services. Manually deleting old files is unsustainable. To maintain server hygiene, you must instruct the find command to automatically locate and permanently eradicate any file that has not been modified within a specified number of days, effectively building a fully automated, surgical disk cleanup pipeline.

Using the find Command with -mtime and -delete

The Linux find command combines the -mtime (modification time) flag with the -delete action to perform a targeted, time-based file purge.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To recursively scan the /var/log/ directory and permanently delete all .log files that have not been modified in over 30 days, type the following command exactly:
  3. sudo find /var/log/ -type f -name "*.log" -mtime +30 -delete
  4. Press Enter.

Surgical Time-Based Eradication

The syntax combines two powerful elements. The -mtime +30 flag instructs the engine to only target files whose modification timestamp is strictly older than 30 full 24-hour periods. The -delete action then permanently removes every matching file from the filesystem without any confirmation prompt. Critically, -delete must always be the last argument in the chain; placing it before -name or -mtime would delete files before the filters are applied, causing catastrophic data loss. This command is an absolute necessity for system administrators building automated cron-based disk maintenance pipelines.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.