PDA

View Full Version : Cron to delete contents of folder?



hamheyelliot
26-03-2011, 06:08 PM
I finished the redux of my old project IconToucan (http://icontoucan.com) today, but I came across the one little pain that's going to stack up.

When a .zip is generated by the site, it gets stored in /zips indefinitely and requires someone to venture into the folder each time and clear it out.

I've never been any good at cron jobs at all, and stuff I've searched for just flies over my head. Does anyone know how I can have the contents of the folder deleted on a daily basis?

All replies appreciated with rep :) I also anticipate it's a tricky job so if you find anything around Google etc. feel free to point it out!

Trinity
26-03-2011, 07:14 PM
Command to use is
:
: /bin/rm -r -f /path/to/directory/*
:
: The format of a crontab file is:
:
: mm hh DD MM WW Command
:
: where
:
: mm = Minute to run the command
: hh = Hour
: DD = Day of the month
: MM = Month
: WW = Day of the week
:
: So use:
:
: 12 4,19 * * * /bin/rm -r -f /path/to/directory/*
:
: "12 4,19" = at 04:12 and 19:12 (outside peak business hours!)
: "* * *" = every day of every month, every day of the week
: "/bin/rm" = run "rm" (ReMove), which is installed in "/bin/"
: "-r" = "recursive mode"
: "-f" = "force mode" (doesn't prompt)
: "/path/to/directory/" = self-explanatory
: "*" = everything inside it, but not the directory itself

Seems pretty helpful.

Found here: http://www.1-script.com/forums/Help-use-a-cron-job-to-delete-contents-of-folder-daily-article111344--2.htm

Source
26-03-2011, 07:23 PM
Why not do a PHP jobby, whenever a new ZIP is generated quickly delete ZIPs that have a 'modified' date of more than a day?

hamheyelliot
26-03-2011, 07:42 PM
Why not do a PHP jobby, whenever a new ZIP is generated quickly delete ZIPs that have a 'modified' date of more than a day?
Sounds typically more useful, only problem is it seems a little excessive if this extra script is ran every single time (at the moment usage is quite frequent). I also wouldn't have any idea where to start :P

I think a scheduled job rather than a dynamic one would suit what I need, so I'll give the cron above a go! Thanks for your help here.

Want to hide these adverts? Register an account for free!