Keeping only 6 logs and deleting the rest...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keeping only 6 logs and deleting the rest...
# 1  
Old 06-15-2009
Keeping only 6 logs and deleting the rest...

Hi Guys,

I have folder where the logs are generated (which is quite a few). I want to keep the recent n logs and delete the rest of them..

I am able to sort them by using ls -t.. but how to take it forward..

Can you people help me..

Thanks for the help in advance,

Regards,
Magesh..
# 2  
Old 06-15-2009
one clunky way:
Code:
#!/bin/ksh
set -A arr  $(ls -t /directory/logfile*)
value=${#arr[*]} 

while [[ value -gt 6 ]] 
do
     x=$(( $value -1 ))  # arrays are zero-indexed
     rm -f ${arr[x]}      # remove the oldest 
     unset arr[x]          # remove the filename from the array
     value=${#arr[*]}   # get new array length
done

try something with arrays like this.
# 3  
Old 06-15-2009
thanks man..i have already created one.. mine goes like this..

#/bin/sh
cd /clocal/dctrdata/user/dctrdat1/sysout
ls -rt > a.out
wc -l a.out|read lines dummy
delcnt=0
if [[ $lines -gt 5 ]]; then
delcnt=$(( lines - 5 ))
if [[ $delcnt -gt 0 ]] ; then
while read rec
do
# chnge the echo to rm -f $rec
echo $rec
delcnt=$(( delcnt -1 ))
[[ $delcnt -eq 0 ]] && break
done < a.out
fi
fi
# 4  
Old 06-16-2009
You could try this command (assuming the names of your logs don't contain newline characters)
Code:
      LOGS_TO_KEEP=5
      ls -t1 | tail -n +$((LOGS_TO_KEEP+1)) | xargs -d '\n' rm -f

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Deleting 3 days old logs from a directory

As i am working in unix environment so i have an logs that is created by my application at the following location that is /opt/app/glac/current/servers/ops/logs inside the logs directory there are different kinds of logs(that is the file having extension as .log ) have been created... (1 Reply)
Discussion started by: 2015nks
1 Replies

3. UNIX for Dummies Questions & Answers

Scripting for deleting logs

Hi, i wants to create the script for my linux server. my system log files are getting huge frequently /root file system getting filled frequently.so wants to delete the logs when it crossed 1GB data on it. Please help me how do i create the bash script.. i use solaris 10 OS... (3 Replies)
Discussion started by: Rahulne25
3 Replies

4. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

5. Shell Programming and Scripting

Deleting repeated lines by keeping only one.

Dear Buddies, Need ur help once again. I have a flat file with around 20 million lines (Huge file it is). However, many of the lines are of no use hence I want to remove it. To find and delete such lines we have certain codes written at the starting of each line. Basis that we can delete the... (2 Replies)
Discussion started by: anushree.a
2 Replies

6. Shell Programming and Scripting

Grep yesterday logs from weblogic logs

Hi, I am trying to write a script which would go search and get the info from the logs based on yesterday timestamp and write yesterday logs in new file. The log file format is as follows: """"""""""""""""""""""""""... (3 Replies)
Discussion started by: harish.parker
3 Replies

7. UNIX for Dummies Questions & Answers

deleting a line but keeping the same file

Hi, I want to delete a line in a file that contains a string. I tried: grep -v "mystring" Myfile > Myfile But this makes the Myfile empty. I read that I need to do something like: grep -v "mystring" Myfile > Myfile.new rm Myfile mv Myfile.new Myfile Is there a way to avoid creating a... (2 Replies)
Discussion started by: laiko
2 Replies
Login or Register to Ask a Question