Linux Bash: how to delete old files and filter by extension

28.06.2023 Published by:

Use find to locate old files, filter by extension and preview matches before deletion. Understand -mtime +30 and avoid deleting newer files inside old directories.

Technical content updated: 15 October 2026. These examples use GNU find, available on Debian and Ubuntu. Run deletion commands only in the intended data directory after checking the matches and retention policy. A backup’s age alone does not mean it is safe to delete.

How do you find files older than 30 days?

First, list matching regular files. Replace the example directory /srv/application-archive with the correct, verified path:

find /srv/application-archive -type f -mtime +30 -print

-type f selects regular files. -mtime refers to the last modification of their contents, not their creation date or last access time.

What exactly does -mtime +30 mean?

GNU find counts complete 24-hour periods and discards the fractional part. -mtime +30 means more than 30 complete periods: in practice, files at least 31 days old match. A file modified 30 days and 12 hours ago does not match yet.

If the retention policy needs a 30-day threshold without rounding to whole days, you can use minutes: -mmin +43200. This is a different condition from -mtime +30; choose deliberately and preview the results again.

Delete only after reviewing the list

Once you have checked the directory and matches, keep the same filters and put -delete at the end:

find /srv/application-archive -type f -mtime +30 -delete

This also deletes matching files in subdirectories. To process only the directory’s first level, add -maxdepth 1 immediately after the path. By default, find does not follow symbolic links; do not add -L unless you intend to change that behavior.

How do you delete only a particular file extension?

For archived application logs, preview the matches first, then use the identical filter with deletion:

find /srv/application-archive -type f -name '*.log' -mtime +30 -print

find /srv/application-archive -type f -name '*.log' -mtime +30 -delete

Quotes stop the shell expanding *.log before find receives it. -name is case-sensitive; use -iname to match .LOG as well. Manage active system logs through their rotation mechanism rather than indiscriminately deleting files from /var/log.

How do you clean up old directories without deleting newer files?

A directory’s modification time does not tell you the age of all files inside it. Combining find -type d -mtime ... with recursive rm -rf can delete newer contents too. It should not be your default retention method.

Separate file deletion based on each file’s own age from empty-directory cleanup. After the first operation, inspect empty directories while excluding the starting directory:

find /srv/application-archive -depth -mindepth 1 -type d -empty -print

After reviewing the results, remove empty directories:

find /srv/application-archive -depth -mindepth 1 -type d -empty -delete

The deletion pass can also remove a parent directory that becomes empty when its empty child is removed. -mindepth 1 protects the starting directory itself. Non-empty directories containing newer files remain.

Retention as part of server maintenance

Combine cleanup with disk-space monitoring and the ability to restore backups. Our Linux server administration service helps adapt maintenance to the application and environment. See also our GIMMIK server administration and technical support project.

Related guide: permanently changing a Debian hostname. Command reference: GNU find – Debian Manpages.

About the author

Leszek Iwaniec is the owner of Projectic. He works on online store implementations, custom applications and technical support. On the blog, he describes problems and solutions involving PrestaShop, Laravel and system integrations, drawing on the team’s project experience.

Back