script help - pull date format out of file name and compare to today
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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.
Last edited by MaureenT; 11-06-2008 at 10:30 AM.
Reason: errors in what I had previously posted and this is more succinct
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.