Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Delete files older than x days; in folder and its subfolders

while leaving the folders/directories in tact

2:34 am on Apr 6, 2012 (gmt 0)

I have a rather large site that has thousands of unused images. I'd like to delete the obsolete ones via script and ideally, send it to /tmp/ so it's easily restorable if it ends up being needed.

The directories are like so:
|_ /thumbs/
|___/thumbs/something/thumb1.jpg or /thumbs/something/thumb2.jpg, etc etc.

The data I'd like to wipe is everything from /images/thumbs/*/ that's older than 90 days, for example.

Is such a thing possible?
I've seen many examples for level 1 deletions, but nothing that (appears to be) easily transformed into 2 or even 3 level removals.
8:07 am on Apr 6, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

If you send them to /tmp you'll also need to purge the files from /tmp as well eventually otherwise you'll end up with a ton of /tmp files and things will start slowing down.

What you want to do is simple, open the directory for /images/thumbs/*/ and iterate through all the files, checking the file status for either the creation date, and purge anything > 90 days old.

Not sure what you mean by level 1, 2 or 3 deletions, file deletions are file deletions., either move them to /tmp or unlink them.

I do something similar to my thumbs but do it on the command line with a couple of simple commands, assuming you're using Linux. First I run a script in PHP that queries the database to find everything currently in use and 'touches' all of the currently used thumbs just to update their current date. After that, I simply purge all files that don't have the current date after running that script.

For instance, for each file being used I shell out and update the date like this per image:
touch -m ../../httpdocs/thumbs/imagename.jpg

Then it takes two simple commands to purge all unused images:
ls -l ../../httpdocs/thumbs/ > allthumbs
rm -f $(grep " Feb 11 " -iv allthumbs | grep "jpg" -i | awk '{print "../../httpdocs/thumbs/"$9}')

Replace " Feb 11 " with your current date in the above script and purge away.

Be careful however, run the GREP portion of that script first to test it before adding the "rm -f $()" wrapper otherwise you could inadvertently blast all of your thumbs by accident.

Requires adult supervision.
9:41 am on Apr 6, 2012 (gmt 0)

incrediBILL - I figured as much. tmp cleanup is the plan after securing a ease-of-use idea/script to purge the inactive images. My current /tmp/ folder is minuscule to-date.

'Opening' the parent folders manually isn't so easy (it is apache).
There's over 3,000+. Checking them manually would take longer than searching for a resolution (they aren't technically hurting anything by existing, but they do cost significant space usage; especially in regards to backups).

I've considered the ssh technique.
However, this particular site will requires constant purging (data and its thumbnails change daily/weekly); would it be more efficient to ssh & purge by hand, or simply cron a script to purge?

What I meant by level 1, 2, etc:
images: level 1
images/thumbnails: level 2
images/thumbnails/something: level 3
etc, etc.
6:55 am on Apr 15, 2012 (gmt 0)

Thanks for the tips, I was able to get it working. :)

Thanks again incrediBILL; with your enormous mountain of knowledge!

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month