Webserver auto delete files

Connect with other users about what to run on your webhosting (and how to run it) here.
Post Reply
User avatar
sevensrequiem
A semi-regular
A semi-regular
Posts: 17
https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
Joined: Sun Mar 31, 2019 5:52 pm
Location: Missouri, USA
Contact:

Webserver auto delete files

Post by sevensrequiem »

I looked up on how to do this and it says to use cron, I've never used cron, so how would I go about doing this?

- I need them to delete every time it exceeds a certain folder size
- I need only .jpg files to be deleted
User avatar
Edge100x
Founder
Founder
Posts: 12945
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Webserver auto delete files

Post by Edge100x »

A shell command like this should do what you want in terms of the actual deletions.
Replace 1000 with the size in bytes that you want to be your threshold for triggering the cleanup.
Replace ~/public/example with the actual folder name that you want to go through (in both places). It should start with ~/ because that is your home folder.

du --max-depth=0 ~/public/example | expr `awk {'print $1'}` \> 1000 && find ~/public/example -name "*.jpg" -delete

To make that into a cron entry, you need to edit your crontab and enter the command prefixed by when you want it run, per the cron documentation. For instance, to make it run every morning at midnight, you could use this entry:

@daily du --max-depth=0 ~/public/example | expr `awk {'print $1'}` \> 1000 && find ~/public/example -name "*.jpg" -delete
Post Reply