purge logs, keep the 30 lasts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting purge logs, keep the 30 lasts
# 1  
Old 08-26-2010
purge logs, keep the 30 lasts

Hi,

I have to make a script to purge logs in a directory (this script will run automatically every day) and this script has to keep just the 30 last files.
So I want it to count all the files in the directory, find the 30 most recents and delete the others.

I just started shell scripting and I'm not very good ^^
I tried this :
Code:
#!/bin/sh
FIND=/opt/csw/bin/gfind
DIR=<path to the directory where logs are>
DIR2=<path where the script is>
CREATE_FILE=`ls -ltr $DIR|cut -c54- > $DIR2/lstFile`;
NB_LINE=`cat $DIR2/lstFile|wc -l`;
deb=`expr $NB_LINE - 30`
FileToDelete=`head -n $deb lstFic> lstFileDel`;
DEL_FILE=`cat lstFileDel`
echo $DEL_FILE
$FIND $DIR -type f -name '$DEL_FILE' -exec rm -f {} \;


but the find rm doesn't work, my variable is good the echo displays the files to delete, but the rm does't work...

so if anyone has an idea?or a better solution (I gues there must be a lot^^)


thanks for the help Smilie

Last edited by Franklin52; 08-26-2010 at 02:38 PM.. Reason: Please use code tags
# 2  
Old 08-26-2010
try this:
Code:
cd <path to the directory where logs are>
rm -f `ls -lt | awk ' NR>30 { print $NF } '`

# 3  
Old 08-26-2010
works great Smilie
I think I made it complicated for nothing

thanks a lot! Smilie
# 4  
Old 08-26-2010
Code:
ls -lt |grep -v ^d | awk ' NR>30 { print $NF } ' |xargs rm

# 5  
Old 08-27-2010
Code:
# rm `ls -1t | sed '1,29d;s/.* \(.*\)$/\1/'`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. UNIX for Dummies Questions & Answers

Apt-get install --purge

Hello, i would like to know what is the use of that command apt-get install --purgeI understand the use of --purge while using with remove parameter, but i don't get it when it's about installing. Enlight me please ! Thx ---------- Post updated 09-03-16 at 11:35 AM ---------- Previous... (0 Replies)
Discussion started by: Purgator
0 Replies

3. Shell Programming and Scripting

Menu for Purge

I have these commands that help me find and delete files over certain days. How can I build a menu to list the files, and then hit y for yes to delete or no? find /logs/212/abinitio/prod/mfs/partitions/part0/mfs_12way_001/mfs_12way/sncrpt/main/ -name "*dat" -mtime +1 -exec ls -ltr {} \; find... (3 Replies)
Discussion started by: xgringo
3 Replies

4. Emergency UNIX and Linux Support

Purge in oracle9i

From morning we are facing some issues in tablespaces in oracle9i. Tried deleting some huge records... but even thought it is still giving tablespcases in full. Then i tried and googling ... found we need to purge recyclebin of oracle.. but thats not woking in oracle9i... can any one... (1 Reply)
Discussion started by: greenworld123
1 Replies

5. Shell Programming and Scripting

script to grep only the lasts errors

Hi, sorry if there already a thread about this, I did a little bit of digging but haven't found exactly what I want. I have a java application on a glassfish server who crash from time to time :D I need a script to allert me if there's a error like "java heappspace" or "out of memory" in the... (5 Replies)
Discussion started by: jblecrou
5 Replies

6. Shell Programming and Scripting

ftp script : list 4 lasts files

Hi, At work we have backups on a ftp. I want to view 4 last files saved (their names, dates, and weight). how can i achieve this goal using simplest way ? Thank's. (3 Replies)
Discussion started by: simon974
3 Replies

7. Shell Programming and Scripting

Remove lasts characters from a string

Hi all, Consider i have a directory /tmp/test and inside this directory i have the following files: 1.svf.tmp 2.svf.tmp 3.svf.tmp How can i remove the last four characters of every file in irder for the directory to be as: 1.svf 2.svf 3.svf I use the following command but id doesn't... (6 Replies)
Discussion started by: chriss_58
6 Replies

8. 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

9. Shell Programming and Scripting

script for purge

Hi , I want to purge 7 days older data from a list of data sorted on date in a log file... Can anyone provide me with the shell script for the same.. Thanks, Jaz (1 Reply)
Discussion started by: JP003
1 Replies

10. UNIX for Dummies Questions & Answers

purge xterm

Being new I had my focus in the wrong place and typed in xterm instead of <If you can't guess, good>. Can I purge xterm so that this word no longer appears using up or down arrows? (1 Reply)
Discussion started by: noobie_doo
1 Replies
Login or Register to Ask a Question

Featured Tech Videos