How To

Find and Delete Files Older Than N Days in Linux

Cleaning up old files is one of those tasks every sysadmin automates sooner or later. Log rotation leaves behind compressed archives, backup scripts pile up daily snapshots, and temporary files accumulate in /tmp. The find command handles all of these with a single line.

Original content from computingforgeeks.com - post 2306

This guide covers practical examples of finding and deleting files older than a specified number of days on any Linux distribution, plus how to automate cleanup with cron.

Find Files Older Than N Days

The find command’s -mtime option matches files by modification time. The value is specified in days: +7 means more than 7 days ago, -7 means less than 7 days ago, and 7 means exactly 7 days ago.

List all files under /var/log that were last modified more than 7 days ago:

find /var/log -type f -mtime +7

Add -ls for detailed output including size, permissions, and modification date:

find /var/log -type f -mtime +7 -ls

Filter by file extension. This finds only .gz compressed log files older than 30 days:

find /var/log -type f -name "*.gz" -mtime +30

Delete Files Older Than N Days

Once you have confirmed the file list looks correct, add -delete to remove them. Always run without -delete first to preview what will be removed.

Delete compressed log files older than 30 days:

find /var/log -type f -name "*.gz" -mtime +30 -delete

The -delete flag is safer than -exec rm because it only removes files (not directories) and does not require shell expansion. For more control, use -exec instead:

find /var/log -type f -name "*.gz" -mtime +30 -exec rm -f {} \;

To see what is being deleted as it happens, add -print before -delete:

find /var/log -type f -name "*.gz" -mtime +30 -print -delete

Common Cleanup Examples

Delete old backup files

Remove database backup dumps older than 14 days from the backup directory:

find /backups -type f -name "*.sql.gz" -mtime +14 -delete

Clean temporary files

Remove all files in /tmp that have not been modified in the last 3 days:

find /tmp -type f -mtime +3 -delete

Delete files by size and age

Remove files larger than 100MB that are older than 7 days. The -size option filters by file size:

find /var/log -type f -mtime +7 -size +100M -delete

Delete empty directories

After deleting files, empty directories may remain. Clean them up with -type d and -empty:

find /backups -type d -empty -delete

Using Access Time vs Modification Time

The find command supports three time-based options:

OptionMeasuresUse case
-mtimeModification time (content changed)Log files, backups, data files
-atimeAccess time (last read)Cache files, temp data
-ctimeChange time (metadata changed)Permission or ownership changes

For most cleanup tasks, -mtime is what you want. Note that -atime can be unreliable on filesystems mounted with noatime (which is common on modern Linux systems for performance).

For finer granularity, use -mmin instead of -mtime to specify minutes instead of days. Delete files modified more than 60 minutes ago:

find /tmp -type f -mmin +60 -delete

Automate Cleanup with Cron

Running cleanup commands manually defeats the purpose. Schedule them with cron to run automatically. Open the root crontab:

sudo crontab -e

Add a job that runs daily at 2 AM to clean up old log files:

0 2 * * * find /var/log -type f -name "*.gz" -mtime +30 -delete

For backup cleanup, run weekly on Sundays at 3 AM:

0 3 * * 0 find /backups -type f -name "*.sql.gz" -mtime +14 -delete

Clean /tmp every 6 hours:

0 */6 * * * find /tmp -type f -mtime +3 -delete 2>/dev/null

The 2>/dev/null suppresses error messages from files that were already deleted by other processes. Redirect output to a log file if you want an audit trail:

0 2 * * * find /var/log -type f -name "*.gz" -mtime +30 -print -delete >> /var/log/cleanup.log 2>&1

Using systemd-tmpfiles as an Alternative

Modern Linux systems include systemd-tmpfiles which provides declarative file cleanup rules. This is the system that manages /tmp cleanup by default on most distributions.

Create a custom cleanup rule in /etc/tmpfiles.d/:

sudo vi /etc/tmpfiles.d/cleanup-backups.conf

Add a rule to delete backup files older than 14 days:

e /backups - - - 14d *.sql.gz

The e directive means “clean up files matching the pattern that are older than the specified age”. Test the rule without making changes:

sudo systemd-tmpfiles --clean --dry-run

The systemd-tmpfiles --clean command runs automatically via a systemd timer, so your rules execute without needing a separate cron entry.

Safety Tips

  • Always preview first – run the find command without -delete to see what matches before deleting
  • Use -type f to avoid accidentally deleting directories
  • Quote glob patterns – use -name "*.gz" not -name *.gz to prevent shell expansion
  • Prefer -delete over -exec rm – it handles edge cases like filenames with spaces
  • Test cron jobs manually first – run the exact command as the cron user to catch permission issues

Related Articles

Databases Install Latest Redis Server on CentOS 7 / RHEL 7 CentOS How To Install pgAdmin 4 on CentOS 8 | RHEL 8 AlmaLinux Install Nextcloud / OwnCloud on Rocky Linux 9|AlmaLinux 9 Asterisk Install Asterisk 18 LTS on CentOS 8 | RHEL 8

Leave a Comment

Press ESC to close