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
-
possible duplicate of [-mtime FIles older than 1 hour](http://stackoverflow.com/questions/543946/mtime-files-older-than-1-hour) – paxdiablo Mar 14 '14 at 07:16
-
1This is for files, not folders. – Joe Scotto Mar 14 '14 at 07:16
-
Then you use `-type d` instead of `-type f`. Bottom line is, you're looking at `find -mtime ...`. – paxdiablo Mar 14 '14 at 07:18
-
If I was to do `find /path/to/files* -type d -mtime +5 -exec rm {} \;` would this delete the folder and contents? – Joe Scotto Mar 14 '14 at 07:19
-
1No, you would have to use `rm -rf`. – paxdiablo Mar 14 '14 at 07:20
4 Answers
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.

- 120,954
- 29
- 226
- 236
-
What would I modify to change the amount of days? The `+1` part? Also this is running on a webserver. – Joe Scotto Mar 14 '14 at 07:19
-
I am using cpanel, for the directory would it be somthing like `public_html/website/uploads` ? – Joe Scotto Mar 14 '14 at 07:21
-
@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. – janos Mar 14 '14 at 07:25
-
-
Is there any website where I could learn more about this? I'm really interested now. – Joe Scotto Mar 14 '14 at 07:29
-
Also, is there a way that I can delete it if it is over a minute or seconds? – Joe Scotto Mar 14 '14 at 07:31
-
@KronoFiles if you have a Linux/UNIX/Mac then you can read about this in `man find`. Or on this site: http://www.linuxmanpages.com/man1/find.1.php – janos Mar 14 '14 at 07:32
-
@KronoFiles it seems you can use `-cmin` and `-mmin` to specify in minutes instead of days. But not in seconds. – janos Mar 14 '14 at 07:34
-
-
@KronoFiles this page explains the difference between ctime and mtime: http://www.linux-faqs.info/general/difference-between-mtime-ctime-and-atime – janos Mar 14 '14 at 07:36
-
I'm running this command to test it but I am getting this: `find: /public_html/kronofiles.com/uploads/1/': No such file or directory` and `find: missing argument to `-exec'` – Joe Scotto Mar 14 '14 at 07:38
-
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

- 271
- 3
- 7
I assume you want to delete a folder with all its content as soon as the newest file within that folder has not been changed within the last 24 hours.
Here is one solution, it's probably not the most elegant:
First, use this command to delete all files whose modification timestamp is older than 24 hours:
find /path/to/the/folder -type f -mtime +0 -print0 | xargs -0 rm
Then, try to delete all folders within your folder, including the folder itself:
find /path/to/the/folder -depth -type d -print0 | xargs -0 rmdir
This will only delete folders that do not contain any files. That is, if files remain after the first step (because their modification timestamp is not older than 24 hours), then your folder (possibly with subfolders), will remain. If the cronjob runs repeatedly, then at one point in time - as soon as no file younger than 24 hours is left within your folder - then your folder and all of its contents will be deleted.
You could put this into a file /etc/cron.d/folder-cleanup as follows:
*/5 * * * * root find /path/to/the/folder -type f -mtime +0 -print0 | xargs -0 rm ; find /path/to/the/folder -depth -type d -print0 | xargs -0 rmdir
This way, your cleanup process would run every 5 minutes.
The downside of this approach is that your folder will vanish step by step. I'm not sure if this is what you need.

- 656
- 6
- 10
-
It emailed me this `find: /public_html/kronofiles.com/uploads/1/': No such file or directory rmdir: missing operand Try rmdir --help' for more information.` – Joe Scotto Mar 14 '14 at 07:49
-
Can you run `find /public_html/kronofiles.com/uploads/1/` and paste the output unto http://pastebin.com/, and tell me the Pastebin link? – Manuel Kießling Mar 14 '14 at 08:45
I have a slightly different recommendation that others might find useful. I want to purge my ~/Downloads folder for files and directories older than 1 day, but I want to send them to the Trash instead. The trash I want to empty at a longer every 180 days automatically on reboot only.
user: crontab -e
:
0 0 * * * find /home/username/Downloads -mindepth 1 -mtime +1 -exec gio trash {} \;
root: sudo crontab -e
:
@reboot find /home/username/.local/share/Trash/expunged/ -type f -mtime +180 -exec rm {} \;
@reboot find /home/username/.local/share/Trash/files/ -type f -mtime +180 -exec rm {} \;
@reboot find /home/username/.local/share/Trash/info/ -type f -mtime +180 -exec rm {} \;

- 1,389
- 11
- 19