Howto delete old directories based on dates in it's dirnames?
Hi,
I have tried to find the solution for my problem on this site and other sites but haven't found a good enough answer yet. Maybe some of you can help me out here? What i need is a script (bash preferrably) that can delete directories based on a date in its dirname. For example. I have a bunch of directories that is named data-20110623/ data-20110624/ data-20110625/ etc. I want to run a cronjob once every day that deletes directories older than 30 days based on its dirname. Anyone wanna share their scriptskills? If so it's is very appreciated! Cheers, Meshuggah |
If you just wanted to delete old files, you could simply use find with -mtime option to find files of certain age and with -exec option to execute rm on them, with needed options. If you insist on a script, I'll throw in some simple example which you can start working on...I'll assume the directories are indeed called data-YYYYMMDD as in your post, and they reside in the working directory (which you could easily change, of course).
Code:
#!/bin/bash 1) take the current date and transform it into Epoch (number of seconds since UTC midnight Jan 1 1970) 2) calculate reference value that is 30 days older than that of today (30*24*60*60 seconds) 2) collect all files (directories) whose name begins with "data-" into an array 3) work through the array - pick up the numerical date-part of the name of each element and transform it into Epoch - compare the current value against the reference value, checking if it's smaller (=older) or not than the reference - do something in either case, in the example simply echo the result. Remember: this relies on the date formats being what they are thought to be. Make sure that it works the way you want before allowing it to remove anything. In fact, rather than directly delete, I'd personally make it move the to-be-removed directories into a "trash directory" first, and empty that a few days later. This would give you an opportunity to save files in case something went wrong. This is a really simplistic example, but you can (and should) modify it to your needs. I suspect that other languages, for example Perl, would make this a lot easier--or then it's just me, but you insisted on bash. Replace the echo statements with actual working stuff (like rm), build a test case (so as not to destroy valuable data in case it does gimmicks), test, and so on. Hope it helped a little. |
Hi,
Thank you for your input. Yes unfortunately i need to rely on "date" in the directory names since atime, ctime, mtime etc. isn't relevant. It's a badly written script that restructures all directories and renaming all of them once every day. And that script is out of my control. So.. The example you gave me is almost what i need. Although I want to list (or remove) only the directories that is 30 days or older. (In this case it will be easy for me to change from listing dirs to removing them) Regards, Meshuggah |
Quote:
There's another reason too not to trust "find" blindly: it if happened that someone (something) for example touch'ed some of the files, their date stamps would change, and they wouldn't then match the find criteria anymore. Or, if you added files and happened to preserve their timestamps that were oldish, you'd put them on the remove list too early. So in that light, this *could* be a more secure way; in any case, if you are afraid of loosing some valuable data, make sure you have backups rolling. See Code:
man date |
The way that I would do it is:
|
Indeed! that's why I avoid one liners and usually go for a 'for file in ...' loop approach, so its easier to read, easier to debug and easier to add extra filtering inside the loop to fine tune name matching if needed.
++everything sundialsvcs said :) |
All times are GMT -5. The time now is 03:23 AM. |