How do i cleanup a log file data every 2 days, I was using below command in a script to remove log file every 2 days, but looks like its not working as log file date and time gets updated every 5 mins. Is there a way ?
Yes, Its a log file for a monitoring cron job, which runs evey 5 mins and updates the log file time each time it updates, so the log file has current date and time.
I want to delete the data in this log file that is 2 days old, how do i do ?
I shall only guide you on one way of achieving the same..you can use this logic if you find it better than ones given by others here...
1>You can get the epoch seconds of the time of creation of the file using stat command
stat -c "%W" file
2>Compare this with the epoch seconds of two days behind..if less than the epoch seconds of 2 days behind...go ahead and delete...
To calculate two days behind epoch seconds
Well I assume, If file is continuously written after 5 mins duration then its updated time also current time only.
Better you provide some details about log files. At least log file date format.
From which we may get some clue to figure out how to achieve this.
Suggestion: I think the best approach here is to re-write your script so that it will create a new log file every day and append to it (use date +"%Y%m%d" to name your file). This way you can use find command to find & delete logs that are more than 2 days old.