Bash Scripting with file date comparison
Hey Guys,
I am new to bash scripting and I need some help. I need to be able to compare a file date with system date and delete files older than 30 days. the file name is basically error_log.03222011 of course the extension is the date the file was created. Any ideas? Oh and before i get hammered I looked everywhere but am unable to make sense of what I found. Thanks again, Michael |
Here's a very simple script I made for this purpose that is very easily portable, since all you have to do is change the BACKUPDIR and DAYSOLD variables to suit your needs.
It's going to indiscriminately delete anything found in the given folder that was created thirty days ago, which makes it simpler than trying to parse on the date stamp. I can do this because part of the process that creates the backups I wrote this script for moves the old ones to a folder reserved just for them. If there are other files in that folder you want to keep, then this wouldn't be the way to go... you could either move them with another command, or modify the find command so it only comes up with the ones you're looking for, by adding the -name switch. Code:
#!/bin/bash |
firstly, always use ISO timestamps, YYYYMMDD
as they are sortable and most sensible people use it. the above find works as long as the timestamp matches the filename. if a body has opened a file and saved it the timestamp may be wrong what I do usually is grab the timestamp of the filename and actually stamp the file to match using touch then the find is guaranteed and you get no surprises. |
Thanks guys I got what I needed from this.
|
All times are GMT -5. The time now is 07:27 AM. |