{"id":2306,"date":"2026-03-24T02:03:36","date_gmt":"2026-03-23T23:03:36","guid":{"rendered":"https:\/\/computingforgeeks.com\/?p=2306"},"modified":"2026-03-24T15:14:45","modified_gmt":"2026-03-24T12:14:45","slug":"find-delete-old-files-linux","status":"publish","type":"post","link":"https:\/\/computingforgeeks.com\/find-delete-old-files-linux\/","title":{"rendered":"Find and Delete Files Older Than N Days in Linux"},"content":{"rendered":"\n<p>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 <code>\/tmp<\/code>. The <code>find<\/code> command handles all of these with a single line.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Find Files Older Than N Days<\/h2>\n\n\n\n<p>The <code>find<\/code> command&#8217;s <code>-mtime<\/code> option matches files by modification time. The value is specified in days: <code>+7<\/code> means more than 7 days ago, <code>-7<\/code> means less than 7 days ago, and <code>7<\/code> means exactly 7 days ago.<\/p>\n\n\n\n<p>List all files under <code>\/var\/log<\/code> that were last modified more than 7 days ago:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -mtime +7<\/code><\/pre>\n\n\n\n<p>Add <code>-ls<\/code> for detailed output including size, permissions, and modification date:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -mtime +7 -ls<\/code><\/pre>\n\n\n\n<p>Filter by file extension. This finds only <code>.gz<\/code> compressed log files older than 30 days:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -name \"*.gz\" -mtime +30<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Delete Files Older Than N Days<\/h2>\n\n\n\n<p>Once you have confirmed the file list looks correct, add <code>-delete<\/code> to remove them. Always run without <code>-delete<\/code> first to preview what will be removed.<\/p>\n\n\n\n<p>Delete compressed log files older than 30 days:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -name \"*.gz\" -mtime +30 -delete<\/code><\/pre>\n\n\n\n<p>The <code>-delete<\/code> flag is safer than <code>-exec rm<\/code> because it only removes files (not directories) and does not require shell expansion. For more control, use <code>-exec<\/code> instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -name \"*.gz\" -mtime +30 -exec rm -f {} \\;<\/code><\/pre>\n\n\n\n<p>To see what is being deleted as it happens, add <code>-print<\/code> before <code>-delete<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -name \"*.gz\" -mtime +30 -print -delete<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Cleanup Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Delete old backup files<\/h3>\n\n\n\n<p>Remove database backup dumps older than 14 days from the backup directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/backups -type f -name \"*.sql.gz\" -mtime +14 -delete<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Clean temporary files<\/h3>\n\n\n\n<p>Remove all files in <code>\/tmp<\/code> that have not been modified in the last 3 days:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/tmp -type f -mtime +3 -delete<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Delete files by size and age<\/h3>\n\n\n\n<p>Remove files larger than 100MB that are older than 7 days. The <code>-size<\/code> option filters by file size:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/var\/log -type f -mtime +7 -size +100M -delete<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Delete empty directories<\/h3>\n\n\n\n<p>After deleting files, empty directories may remain. Clean them up with <code>-type d<\/code> and <code>-empty<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/backups -type d -empty -delete<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Access Time vs Modification Time<\/h2>\n\n\n\n<p>The <code>find<\/code> command supports three time-based options:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Option<\/th><th>Measures<\/th><th>Use case<\/th><\/tr><\/thead><tbody><tr><td><code>-mtime<\/code><\/td><td>Modification time (content changed)<\/td><td>Log files, backups, data files<\/td><\/tr><tr><td><code>-atime<\/code><\/td><td>Access time (last read)<\/td><td>Cache files, temp data<\/td><\/tr><tr><td><code>-ctime<\/code><\/td><td>Change time (metadata changed)<\/td><td>Permission or ownership changes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For most cleanup tasks, <code>-mtime<\/code> is what you want. Note that <code>-atime<\/code> can be unreliable on filesystems mounted with <code>noatime<\/code> (which is common on modern Linux systems for performance).<\/p>\n\n\n\n<p>For finer granularity, use <code>-mmin<\/code> instead of <code>-mtime<\/code> to specify minutes instead of days. Delete files modified more than 60 minutes ago:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/tmp -type f -mmin +60 -delete<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Automate Cleanup with Cron<\/h2>\n\n\n\n<p>Running cleanup commands manually defeats the purpose. Schedule them with cron to run automatically. Open the root crontab:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo crontab -e<\/code><\/pre>\n\n\n\n<p>Add a job that runs daily at 2 AM to clean up old log files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 2 * * * find \/var\/log -type f -name \"*.gz\" -mtime +30 -delete<\/code><\/pre>\n\n\n\n<p>For backup cleanup, run weekly on Sundays at 3 AM:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 3 * * 0 find \/backups -type f -name \"*.sql.gz\" -mtime +14 -delete<\/code><\/pre>\n\n\n\n<p>Clean <code>\/tmp<\/code> every 6 hours:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 *\/6 * * * find \/tmp -type f -mtime +3 -delete 2>\/dev\/null<\/code><\/pre>\n\n\n\n<p>The <code>2>\/dev\/null<\/code> suppresses error messages from files that were already deleted by other processes. Redirect output to a log file if you want an audit trail:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 2 * * * find \/var\/log -type f -name \"*.gz\" -mtime +30 -print -delete >> \/var\/log\/cleanup.log 2>&1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using systemd-tmpfiles as an Alternative<\/h2>\n\n\n\n<p>Modern Linux systems include <code>systemd-tmpfiles<\/code> which provides declarative file cleanup rules. This is the system that manages <code>\/tmp<\/code> cleanup by default on most distributions.<\/p>\n\n\n\n<p>Create a custom cleanup rule in <code>\/etc\/tmpfiles.d\/<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/tmpfiles.d\/cleanup-backups.conf<\/code><\/pre>\n\n\n\n<p>Add a rule to delete backup files older than 14 days:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>e \/backups - - - 14d *.sql.gz<\/code><\/pre>\n\n\n\n<p>The <code>e<\/code> directive means &#8220;clean up files matching the pattern that are older than the specified age&#8221;. Test the rule without making changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemd-tmpfiles --clean --dry-run<\/code><\/pre>\n\n\n\n<p>The <code>systemd-tmpfiles --clean<\/code> command runs automatically via a systemd timer, so your rules execute without needing a separate cron entry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Safety Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Always preview first<\/strong> &#8211; run the <code>find<\/code> command without <code>-delete<\/code> to see what matches before deleting<\/li>\n\n<li><strong>Use <code>-type f<\/code><\/strong> to avoid accidentally deleting directories<\/li>\n\n<li><strong>Quote glob patterns<\/strong> &#8211; use <code>-name \"*.gz\"<\/code> not <code>-name *.gz<\/code> to prevent shell expansion<\/li>\n\n<li><strong>Prefer <code>-delete<\/code> over <code>-exec rm<\/code><\/strong> &#8211; it handles edge cases like filenames with spaces<\/li>\n\n<li><strong>Test cron jobs manually first<\/strong> &#8211; run the exact command as the cron user to catch permission issues<\/li>\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>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. This guide covers practical examples of finding and deleting files older &#8230; <a title=\"Find and Delete Files Older Than N Days in Linux\" class=\"read-more\" href=\"https:\/\/computingforgeeks.com\/find-delete-old-files-linux\/\" aria-label=\"Read more about Find and Delete Files Older Than N Days in Linux\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":2307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[299,50],"tags":[412,36972],"class_list":["post-2306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","category-linux-tutorials","tag-find","tag-find-files-on-linux"],"_links":{"self":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/2306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/comments?post=2306"}],"version-history":[{"count":1,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/2306\/revisions"}],"predecessor-version":[{"id":163991,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/2306\/revisions\/163991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media\/2307"}],"wp:attachment":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media?parent=2306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/categories?post=2306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/tags?post=2306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}