How to delete folders that are older than a day? (Cron Job)
12,347
Solution 1
This should do it:
find /path/to/dir -maxdepth 0 -ctime +1 -exec rm -fr {} +
But be careful, and test it first outside of cron
, without the -exec
part, so you don't delete something else by accident.
Solution 2
First see which files will be deleted:
find /path/to/dir -type d -mtime +1 -print
Then once ready to execute:
find /path/to/dir -type d -mtime +1 -print0 | xargs -0 rm -r
Related videos on Youtube
Author by
Joe Scotto
Writer of code, creator of videos, and taker of photos.
Updated on August 11, 2022Comments
-
Joe Scotto over 1 year
I want to delete a folder and its contents on my host if it is over a day old. The issue is that I have no experience on how to do this with a linux cron job. I should also say that i have looked at google and nothing comes up. I think this is a pretty simple question so please, help me out. Thanks
-
Joe Scotto about 10 yearsThis is for files, not folders.
-
paxdiablo about 10 yearsNo, you would have to use
rm -rf
.
-
-
Joe Scotto about 10 yearsWhat would I modify to change the amount of days? The
+1
part? Also this is running on a webserver. -
Joe Scotto about 10 yearsI am using cpanel, for the directory would it be somthing like
public_html/website/uploads
? -
janos about 10 years@KronoFiles that really depends on your hosting provider. If a command has output in cron, then cron usually emails the output to use. You can run
find public_html/website
without any other arguments to print all the files and subdirectories in there and it should get emailed to you. Based on that output you can figure out the right path to use. -
Joe Scotto about 10 yearsWould I fill my directory in in both
/path/to/dir
? -
Joe Scotto about 10 yearsIs there any website where I could learn more about this? I'm really interested now.
-
Joe Scotto about 10 yearsAlso, is there a way that I can delete it if it is over a minute or seconds?
-
janos about 10 years@KronoFiles if you have a Linux/UNIX/Mac then you can read about this in
man find
. Or on this site: linuxmanpages.com/man1/find.1.php -
janos about 10 years@KronoFiles it seems you can use
-cmin
and-mmin
to specify in minutes instead of days. But not in seconds. -
Joe Scotto about 10 yearsDoes it matter which one? and how would I structure it?
-
janos about 10 years@KronoFiles this page explains the difference between ctime and mtime: linux-faqs.info/general/…
-
Joe Scotto about 10 yearsI'm running this command to test it but I am getting this:
find: /public_html/kronofiles.com/uploads/1/': No such file or directory
andfind: missing argument to
-exec'` -
Joe Scotto about 10 yearsIt emailed me this
find: /public_html/kronofiles.com/uploads/1/': No such file or directory rmdir: missing operand Try rmdir --help' for more information.
-
janos about 10 years@KronoFiles I knew you would figure that one out ;-)
-
Manuel Kießling about 10 yearsCan you run
find /public_html/kronofiles.com/uploads/1/
and paste the output unto pastebin.com, and tell me the Pastebin link?