Deletion of Logs

Can someone suggest on this script :

let's say the logs files are available for Jan 07, Dec 06, Nov 06, Oct 06

the script should identify the latest months logs, i.e Jan 07

it should then delete anything older than 2 months, which will be logs for Nov 06 & Oct 06.

Can someone suggest on this script :

let's say the logs files are available for Jan 07, Dec 06, Nov 06, Oct 06

the script should identify the latest months logs, i.e Jan 07

it should then delete anything older than 2 months, which will be logs for Nov 06 & Oct 06.

you could make use of the datecalc available in the forum - to do all sorts of operations with date.

I have a question to you :slight_smile:

Why do you want to duplicate the post?

Could you please redirect me there(datecalc) ... Thanks .

thats a famous one

Please search the forums :slight_smile:

datecalc is not there in our Production environment...

" Is it possible to see if there are files/dirs where there are difference of 2 months and delete the older once "

Thanks again

A simple method with find would be:
find dirname -mtime +60 -print
will give you all files and directories modified more than 60 days ago.

If running on Solaris you could check out "logadm"

I think u r missing my point :

I know how to get logs deleted after x number of days/months

What I need is :

" let's say the logs files are available for Jan 07, Dec 06, Nov 06, Oct 06

the script should identify the latest months logs, i.e Jan 07

it should then delete anything older than 2 months from Jan 2007 , which will be logs for Nov 06 & Oct 06."

I hope you get my Point, Suppsoe the logs are created only for Nov 2006 then it should not delete anything...

Using the "-t" option of "ls" you can list files sorted by the (modification) time stamp.
Using the "-E" option of "ls" you can have all time stamps displayed in the same format.

e.g.
ls -tE | sed 1d | head -1 | nawk '{ print $6 }'

Will display the modification date, in yyyy-mm-dd format, of the file which was modified most recent.

Using some date calculation (see other threads) you can find out how many days ago this file was modified.

Once you know this offset you can add to it the number of days representingg the 2 months. You can take just 60 for the 2 months or calculate it exactly considering whether the 2 months before had 28, 29, 30 or 31 days.

Once you have a new offset you can use "find . -mtime +(newoffset)" to remove the files.

Today it is Feb 14, 2007
Suppose your most recent log file was created on Jan 28, 2007

with ls -tE ............ you can find the date 2007-01-28

Using date math you can find out 2007-01-28 is 17 days ago compared to today.

Add the 60 (or 61, 31 for dec and 30 for nov) days to 17 resulting in 77 (or 78).

An offset of 78 days from today backwards is Nov 28, 2006

Using find . -mtime +77 ............. you can delete all files last modified before Nov 28, 2006

This would remove all file 2 months older than Jan 28, 2007

Thanks I was looking for Something Like this only ...
but you know what I am getting this done on a SUN box

Heres what I get with the ls option :

bash-2.03$ ls -tE
ls: illegal option -- E
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]
bash-2.03$

Many Thanks any suggestions welcome ... While I am looking in to it :slight_smile:

Solaris 10 has the -E option, apparently you are running on a lower version.

Based on "ls -lt" you can get the information as well, just requires a bit more parsing.

Hey thanks again

bash-3.00$ ls -ult --full-time| sed 1d | head -1 | awk '{ print $6 }'
2007-02-15

I am working on the above date calculation ... let me know more if you can

Thanks again

Can the below be used? :slight_smile:

ls -lult | awk '/^-/ {print $6; exit}'

Thanks Madan,

I had done this but now I am working on date math to find out the 60 days ago dates it should delete as compared to the lateset file which was 2007-01-13, which means 13-october-2006... any ideas

datecalc i dont have

bash-2.03$ datecalc
bash: datecalc: command not found

bash-2.03$ /bin/datecalc
bash: /bin/datecalc: No such file or directory

datecalc is not a command

its a script that is available handy in our forums - search for datecalc :slight_smile:

The full script for datacalc is posted at the link I provided.

Yes , I saw that ....

I am not able to understand how this datecalc can be included with our needs :slight_smile: