homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Delete files older than x days; in folder and its subfolders
while leaving the folders/directories in tact
brokaddr



 
Msg#: 4437820 posted 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:
/images/
|_ /thumbs/
|__/thumbs/something/
|___/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.

 

incrediBILL

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



 
Msg#: 4437820 posted 8:07 am on Apr 6, 2012 (gmt 0)

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.

brokaddr



 
Msg#: 4437820 posted 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.

brokaddr



 
Msg#: 4437820 posted 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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved