{"id":768,"date":"2011-08-02T12:37:46","date_gmt":"2011-08-02T17:37:46","guid":{"rendered":"http:\/\/it.thelibrarie.com\/weblog\/?p=768"},"modified":"2011-08-02T12:37:46","modified_gmt":"2011-08-02T17:37:46","slug":"delete-files-older-than-x-days","status":"publish","type":"post","link":"https:\/\/it.thelibrarie.com\/weblog\/2011\/08\/delete-files-older-than-x-days\/","title":{"rendered":"Delete Files Older Than X Days"},"content":{"rendered":"<p>I was tasked with auto removing files from a linux FTP site that were older than 14 days.  Unfortunately the files were all located in the ~ (home) directories.<\/p>\n<p>I originally tried:<br \/>\n<code>find \/home\/* -mtime +14 -type f<\/code><br \/>\nThis command attempts to find ALL (*) files in the home (\/home\/) directory with a modified time of 14 days (-mtime +14) and only files not directories (-type f)<\/p>\n<p>The command found all the files ok, but it was also listing the hidden files:<\/p>\n<blockquote><p>\/home\/user\/.profile<br \/>\n\/home\/user\/.bash_logout<br \/>\n\/home\/user\/.bash_history<br \/>\n\/home\/user\/.bashrc<\/p><\/blockquote>\n<p>So I decided to use regex:<br \/>\n<code>find \/home\/* \\( ! -regex '.*\/\\..*' \\) -type f -mtime +14<\/code><br \/>\nThis worked!<\/p>\n<p>Now to add the removal of the files:<br \/>\n<code>find \/home\/* \\( ! -regex '.*\/\\..*' \\) -type f -mtime +14 -exec rm {} \\;<\/code><\/p>\n<p>I saved this as delete_files, then made it runnable, and cron&#8217;d it up:<br \/>\n<code>chmod +x delete_files<\/code><br \/>\n<code>crontab -e<\/code><br \/>\n<code>@daily .\/delete_files #Delete Files Older Than 14 Days in Home Dirs<\/code><\/p>\n<p>This worked on my Ubuntu 10.04.3 LTS system, but the commands should work on all the flavors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was tasked with auto removing files from a linux FTP site that were older than 14 days. Unfortunately the files were all located in the ~ (home) directories. I originally tried: find \/home\/* -mtime +14 -type f This command attempts to find ALL (*) files in the home (\/home\/) directory with a modified time &hellip; <a href=\"https:\/\/it.thelibrarie.com\/weblog\/2011\/08\/delete-files-older-than-x-days\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Delete Files Older Than X Days<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-768","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/posts\/768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/comments?post=768"}],"version-history":[{"count":1,"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":769,"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/posts\/768\/revisions\/769"}],"wp:attachment":[{"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/media?parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/categories?post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/it.thelibrarie.com\/weblog\/wp-json\/wp\/v2\/tags?post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}