script help - pull date format out of file name and compare to today
I'm looking for some assistance in pulling a date format out of a file name and comparing it to the current date along with a retention value to determine whether or not to delete the file.
I'm reading 3 values from a .csv file 1. path to files 2. common naming convention of files 3. retention in days .csv file would look like this: /home/myhome/,logtype1_,4 /logs,logtype2_,20 filenames are in format logtype1_yymmdd.log I want to pull out the yymmdd, compare it to todays date and the retention time and determine whether or not to delete it. I just need to strip off the common name which is defined and .log. The values in the .csv file get read into following variables: PTH NAME DAYS I can easily find the files by that name in the path using this cmd: find $PTH -name $NAME* I need help using probably awk or sed to strip the date out of the filename, compare it and remove the file if it's older than the retention period. it would be ideal if I could do it on 1 line with a find -exec cmd. I don't want to simply compare dates using a find function on create or modify date for a whole folder. I want to be specific about the files being removed. If you post code back could you try to provide a bit of an explanation as to how the code works. Thanks. |
Since you have a well defined filename, you can use parameter substitution to dig out the items you need right in a bash shell. See Parameter Expansion in man bash.
#!/bin/sh # your_process FILENAME FILENAME=${1%.*} # this is the filename without the .log LOGTYPE=${1%_*} # this is everything up to the _ in the filename DATE=${1: -10:6} # this is the six characters starting 10 in from the end Dave |
This will give you today's date in same format:
today=`date +%y%m%d` then all you need to do is compare (subtract) the 2 values |
Still having issues
So I've tried to play around with your examples and do some reading but I'm not providing filenames as variables to a script.
I'm trying to do something like this: For FILE in 'find $PTH - name $NAME*`; do FDATE=method to strip yyyymmdd out of FILE compare FDATE to TODAY if diff greater than retention then rm $FILE done I need a method to read FILE and strip out dates. I'm looking at cut, sed, awk?? Maybe something like echo $FILE | sed -e s/^logname_// Just trying to read up on sed and awk now. I'm guessing I may also have to do something to convert FDATE from string to value for comparison to TODAY? |
Get it
Sorry, I get your substitution example now. Found something similar that put it together for me.
Here is my test example that works: for FILE in `find $PTH -name $NAME*` do FDATE=${FILE: -12:8} echo $FILE echo $FDATE done This will echo 8 characters starting 12 characters from end of file name so as long as your filenames are always something_yyyymmdd.log, it will pull out yyyymmdd portion. Thanks for assistance. |
Good. Now you also said you'd like a single command. After you get the script working, you can save it in a local location like /usr/local/bin/ and, if you use full path names (cron's environment is unique), you can then put a rotation routine like this in your crontab using a single line to call it.
Parameter substitution is seldom used but very powerful and works well in applications like yours. One of the good features of the bash shell, IMHO. Dave |
All times are GMT -5. The time now is 12:54 AM. |