Kron Shell: deleting all but most recent log files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kron Shell: deleting all but most recent log files
# 1  
Old 11-06-2008
Kron Shell: deleting all but most recent log files

I am trying to create a Korn Shell script to be run every 5-10 minute from a crontab. This script needs to look for log files (transaction_<date>.log). If there are more than 5 such files, it needs to delete all but the most current 5. How often these files are create varies - can be every minute can be once a day, but it is important to leave the couple most current files. Conceptually I know what to do, but syntactically can't pull it off. Seems like it needs to do a ls -ltr (return the files in date order most recent being last). It needs to know how many were returned,. If it isn't more than 5, don't do anything else. If there are, get the files names of the all the logs except the last 5 returned by ls -ltr and delete them. But How to do all this?
# 2  
Old 11-06-2008
Code:
ls -rt *.txt > tmp.tmp
wc -l tmp.tmp | read lines dummy
delcnt=0
[[ $lines -gt 5  ]] && 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 < tmp.tmp
fi

Verify this works as required before you start deleting files
# 3  
Old 11-06-2008
Thank you. Wow, I'd never have gotten to that elegant of a script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell : deleting only first 2 files in a directory

I have 4 files in a directory and want to delete only first 2 files only.. $ ls -ltr total 640 -rw-r--r-- 1 user other 148779 Oct 12 10:50 file1.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file2.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file3.xls... (4 Replies)
Discussion started by: giridhar276
4 Replies

2. Homework & Coursework Questions

Problem with Shell Scripts deleting text in files.

Me and a friend are working on a project, and We have to create a script that can go into a file, and replace all occurances of a certain expression/word/letter with another using Sed. It is designed to go through multiple tests replacing all these occurances, and we don't know what they will be so... (1 Reply)
Discussion started by: Johnny2518
1 Replies

3. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

4. AIX

List only the recent log files

Hi Friends, I have a list of files in a directory as shown below. It is basically in this format-> yymmdd.hhmmss.filename.out I want to list the latest log of each file. ie. the lastest a.out, b.out, c.out, which means I am looking for only the below 3 files out of these 5 files: ... (3 Replies)
Discussion started by: sudvishw
3 Replies

5. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

6. Shell Programming and Scripting

shell script reqd - deleting files

I have written a script that deletes files: Requirement: i need to delete the files and to know how many files are deleted i.e the count of files and even i need to display when the started time of deletion and the ending time of deletion. I need to display these two times. script: ... (2 Replies)
Discussion started by: venkatesht
2 Replies

7. Shell Programming and Scripting

shell script: deleting files from a directory

i have the following problem: use shell script: Two directories have to be searched for files havin the same name. then delete these files from one of these directories. the directory names have to be accepted as command line arguments. what i have done till now is: 1. run a loop... (1 Reply)
Discussion started by: onlyc
1 Replies

8. Shell Programming and Scripting

CRON-Job / Shell-Skript deleting special files

Just I'm trying to find script, which will do the following job: 1. as a CRON-Job it shoult a) delete files which will be either older than 24 hours or b) all files within a) a directory deleting recursive b) only a special directory. 2. write an error-/Delete_log... (9 Replies)
Discussion started by: ManfredWL
9 Replies

9. UNIX for Dummies Questions & Answers

deleting 100k log files quickly

I've walked into a while loop gone bad.....which has created 100k+ log files. Is it quicker removing the files with rm pattern* or actually removing the entire directory with rm -rf dir/ It's taking ages (hours) either way ...just curious if one is goig to be quicker than the other...or is it... (8 Replies)
Discussion started by: peter.herlihy
8 Replies

10. UNIX for Dummies Questions & Answers

deleting log files only in particular directories

Hi My problem is i have to remove some log files in specific named directories on a regular basis using shell scripts. What i want my shell script to do is i give the shell script some listing of directories from which to delete all log files recursively. Can anyone please help me. ... (2 Replies)
Discussion started by: sameervs
2 Replies
Login or Register to Ask a Question