Deleting dated log files whihc are older than an year

Hi. There is a process which creates log files in the below naming format processname.20140425.log processname1.20140425.log And in this path along with these logs there are other files which have the timestamp from 2012 and i want to have a script in place to delete only the above log files which are older than an year. please help me with a script please.

Any attempts from your side? Did you consider searching these forums?

i tried using the find command but it is showing all the .log files.. including files in subdirectories which is not required. thats where i am struck

man find :

sorry.. i did not understand this..

i did a grep to list the valid files which need to be checked whether they are greater than an year and if greater delete them. need help here

 ls -ltr | grep 'Ft*.*.log
-rw-r--r-- 1 root root     250407 Apr 25  2014 Ftprocessname.20140425.log
-rw-r--r-- 1 root root  29600684 Apr 25  2014 Ftprocessname1.20140425.log
-rw-r--r-- 1  root root   2542462 Apr 25  2014 Ftprocessname2.20140425.log
-rw-r--r-- 1  root root   1324375 Apr 25  2014 processname1.20140425.log
-rw-r--r-- 1  root root   5593542 Apr 25  2014 processname2.20140425.log
-rw-r--r-- 1  root root   5593542 Apr 25  2014 processname1.20140425.log
....
.
.
.
.

Even if you had matching single quotes in your grep command, the last three lines shown in your output would not be there because a line that does not contain the string Ft will not match the BRE passed to grep as 'Ft*.*.log' . So how did you really get the output above???

Is the one year you're talking about supposed to be based on the time the file was last modified (as implied by ls -ltr ) or by the date stamp in the file's name?

If you want to use the last modified time:

find . -name 'Ft*.*.log' -mtime +364 -exec echo rm {} +

should come close to what you want with find .

If you want to use the date stamp in the file's name, try something like:

#!/bin/ksh
EDD=$(($(date +%Y%m%d) - 10000))
printf 'Looking for files no newer than %s\n' "$EDD"
for fn in Ft*.*.log
do	FD=${fn%.*}
	FD=${FD##*.}
	if [ "$FD" -le $EDD ]
	then	echo rm "$fn"
	fi
done

I used the Korn shell for this, but it will work with any POSIX-conforming shell (or any shell that is based on Bourne shell syntax that supports arithmetic substitutions).

To actually remove the files instead of just get a list of the commands that will be used, remove the echo in both scripts after you have convinced yourself that that script is selecting the files you want to delete correctly.

1 Like

Hi Don,

Please ignore that. i have renamed the original files for few reasons and i think i missed to rename these sorry for that confusion.

regarding

find . -name 'Ft*.*.log' -mtime +364 -exec echo rm {} +

this will remove all the logs more than 364 days, including the the ones in sub directories which is not expected.

I will try with the ksh script you gave and get back to you.. Thanks much!!

Why don't you give it a try (by just printing the resulting file names)? It will make find search only the uppermost directory.

1 Like

As I said before, use the for loop in the shell if you want to use the date in the filename to determine which files are a year old and use find if you want to use the last modification time of the file to determine which files are a year old.

The shell for loop I provided only looks at files in the current directory. If you want to use find and only want to look at files in the current directory, you could try what RudiC suggested:

find Ft*.*.log -mtime +364 -exec echo rm {} +

but, if there are LOTS of files matching that pattern, you might fail with an E2BIG error. And, if there are subdirectories in the current directory that have the same filename format as the log files you want to process, it will search those directories for log files to process. If either of these things is a problem you can avoid both of them with:

find . \( -type d ! -name "." -prune \) -o  -name 'Ft*.*.log' -mtime +364 -exec echo rm {} +
1 Like

Don,
Apologize for the delayed response, struck in another issue.
The KSH ran as expected for me. Thanks a lot :b::)..

And may be this is very basic to ask... just want to know about this command.

EDD=$(($(date +%Y%m%d) - 10000))

how did we arrive at 10000. wanted to learn this calculation, as in case if i need to modify this script to look for files before a month or so.

Thanks in advance !!!

date +%Y%m%d
20151022

is just a number. Subtracting 10000 will result in a number representing one year ago. (May fail on certain dates, e.g. 29. Feb.)

1 Like

Thanks Rudi..

Got the logic.. Thanks Again Rudi..

I wasn't trying to create a valid date a year ago; I was just looking for a number representing a year ago that we can compare to a number representing some other arbitrary date. Except for leap days, the number produced is a valid representation of the number produced in the same way for the date exactly one year ago.

And, when run on a leap day, it will still yield a value that can be compared against a valid date string approximately one year ago and get reasonable results from a numeric comparison. The numeric representation of March 1 of the previous year will be greater than the numeric representation of February 29th a year ago even though there was no February 29 a year ago. And the numeric representation of February 28 of the previous year will still be less than the representation of February 29th a year ago even though there was no February 29 a year ago.

1 Like

Thank you Don..Understood.