Cleanup a log file data every 2 days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cleanup a log file data every 2 days
# 1  
Old 10-26-2012
Cleanup a log file data every 2 days

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 ?

Code:
 find ./ \( -name . -o -prune \) -type f -mtime -2 -name /var/log/wpar_monitor_2days.log -exec rm -f {} \;

# 2  
Old 10-26-2012
So are you saying that some process is still writing to this 2 days old log file every 5 mins?
# 3  
Old 10-26-2012
Quote:
Originally Posted by bipinajith
So are you saying that some process is still writing to this 2 days old log file every 5 mins?
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 ?
# 4  
Old 10-26-2012
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
Code:
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
Code:
$((`date +"%s"` - 172800))

# 5  
Old 10-26-2012
msabhi@ giving error
# stat -c "%W" wpar_monitor_2days.log
ksh: stat: not found

Quote:
Originally Posted by msabhi
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
Code:
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
Code:
$((`date +"%s"` - 172800))

# 6  
Old 10-26-2012
Could you calculate the number of lines added to log file every day ?
Then you can use csplit to split the file and delete the the other one.
# 7  
Old 10-26-2012
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.

ThanksSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find a file that's modified more than 2 days ago but less than 5 days ago?

How to find a file that's modified more than 2 days ago but was modified less than 5 days ago by use of any Linux utility ? (4 Replies)
Discussion started by: abdulbadii
4 Replies

2. Shell Programming and Scripting

Filtering log file with lines older than 10 days.

Hi, I am trying to compare epoch time in a huge log file (2 million lines) with todays date. I have to create two files one which has lines older than 10 days and another file with less than 10 days. I am using while do but it takes forever to complete the script. It would be helpful if you can... (12 Replies)
Discussion started by: shunya
12 Replies

3. OS X (Apple)

Text file cleanup

Hi there, i do get some text files that i'd lile to clean them up based on following rule: if a line starts with " then remove return (new line, carriage return) before ". Example, my input text file line 1 line 2 "line 3 I'd like this to come as line 1 line 2line3 How can i... (4 Replies)
Discussion started by: gigagigosu
4 Replies

4. Shell Programming and Scripting

Collect last 2 days data from /var/log/messages

I need to collect last 2 days data from /var/log/messages into a separate file (file format: flmessagetimedaymonth). I have collect today's month, date, time information in separate variable. Please help me in this issue (Probably need awk and grep function). month=$(date|awk '{print $2}')... (4 Replies)
Discussion started by: makauser
4 Replies

5. UNIX for Dummies Questions & Answers

Find all log files under all file systems older than 2 days and zip them

Hi All, Problem Statement:Find all log files under all file systems older than 2 days and zip them. Find all zip files older than 3days and remove them. Also this has to be set under cron. I have a concerns here find . -mtime +2 -iname "*.log" -exec gzip {} Not sure if this will work as... (4 Replies)
Discussion started by: saurabh.mishra
4 Replies

6. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

7. Shell Programming and Scripting

extract information from a log file (last days)

I'm still new to bash script , I have a log file and I want to extract the items within the last 5 days . and also within the last 10 hours the log file is like this : it has 14000 items started from march 2002 to january 2003 awk '{print $4}' < *.log |uniq -c|sort -g|tail -10 but... (14 Replies)
Discussion started by: matarsak
14 Replies

8. Shell Programming and Scripting

awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted. This happened due to manual manipulation of /etc/passwd files. I need to do this for 40 servers. Can anyone help me in achieving this? Even reducing a step or two will be... (6 Replies)
Discussion started by: pdtak
6 Replies

9. Shell Programming and Scripting

Add data in days:hours:min:sec format

I want to add these no. these are in the format of days:hours:minutes:sec I want result in this format only 0:00:04:59 0:00:00:12 0:00:00:28 0:00:00:03 0:01:29:35 0:00:00:19 0:01:05:21 Is any body ca help me????? To get This.. Thanks Nishant (1 Reply)
Discussion started by: krishna_sicsr
1 Replies

10. Shell Programming and Scripting

Maintain 30 days data

Hi Folks, I have a log file that keeps the information pn the date and time a specific transaction is firedup. I found that the log file keeps growing and I intend to limit the entry to the log file to 30 days. Log file name is transaction.log, here is the content: 120802_23:47:37 ... (3 Replies)
Discussion started by: odogbolu98
3 Replies
Login or Register to Ask a Question