delete files except most recent | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

delete files except most recent

Shell Programming and Scripting


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-21-2005
ST2000 ST2000 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 21 June 2005, 2:33 PM EDT
Location: Toronto
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
delete files except most recent

#!/bin/ksh -xvf

for arch_filename in `ls -lrt /u02/oracle/CMDR/archive | awk '{print $9}'`; do
echo "rm -rf /u02/oracle/CMDR/archive/"$arch_filename
rm -rf /u02/oracle/CMDR/archive/$arch_filename
done


I am running the above shell script every 10 minutes. I need to not delete the most recent file, but delete everything else. Each file is 500 mb. How would I do it. Basically when job kicks off, some files are getting deleted as it is writing the archive log and system is getting filled up and I cant see where the files are written to. I have to bounce the box to see the space. This is linux box.

Thanks, ST2000
Sponsored Links
    #2  
Old 06-21-2005
pixelbeat pixelbeat is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 12 January 2006, 7:33 AM EST
Location: Ireland
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
DIR=/u02/oracle/CMDR/archive

find $DIR -type f -maxdepth 1 -printf "%T@\t%p\n" | #list files with timestamps
sort -k1,1n | #sort oldest -> newest
cut -f2- | #remove timestamps
sed '$d' | #remove the last one (newest file)
tr '\n' '\0' | #delimit with NULs in case spaces in names
xargs -r0 rm -f

Also have a look at my newest script
Sponsored Links
    #3  
Old 06-21-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
 
Join Date: Mar 2005
Last Activity: 20 September 2013, 10:59 AM EDT
Location: Philadelphia metro
Posts: 958
Thanks: 3
Thanked 66 Times in 59 Posts
try ... (in ksh) ...

Code:
DIR=/u02/oracle/CMDR/archive

cd $DIR
lastfile=$(ls -rt | tail -1)
for afile in $(ls | grep -v $lastfile)
do
    echo > $afile
    rm -rf $afile
done

    #4  
Old 06-21-2005
dangral dangral is offline Forum Advisor  
Registered User
 
Join Date: Oct 2002
Last Activity: 31 July 2013, 6:52 PM EDT
Posts: 723
Thanks: 3
Thanked 4 Times in 4 Posts
Quote:
Originally Posted by Just Ice
try ... (in ksh) ...

Code:
DIR=/u02/oracle/CMDR/archive

cd $DIR
lastfile=$(ls -rt | tail -1)
for afile in $(ls | grep -v $lastfile)
do
    echo > $afile
    rm -rf $afile
done

Can you explain why you empty the file before deleting it? Is that faster?
Sponsored Links
    #5  
Old 06-21-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
 
Join Date: Mar 2005
Last Activity: 20 September 2013, 10:59 AM EDT
Location: Philadelphia metro
Posts: 958
Thanks: 3
Thanked 66 Times in 59 Posts
i've found that when the filesystem is short on space, rm will fail and/or take longer to complete --- emptying the file lets me get around the space issue ... yes, i think it is faster considering the alternatives ...
Sponsored Links
    #6  
Old 06-21-2005
ST2000 ST2000 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 21 June 2005, 2:33 PM EDT
Location: Toronto
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for all the posts.

Each file is about 500mb. If the delete job kicks off and if there is a file written at that time, linux does not know what to do and locks that space off. Say 200 mb is written when delete kicks off, that 200mb is gone from archive filesystem and in a week's time it reduces to nothing and will not last even an hour. I have to bounce db to clear up pending archives. Instead i want a modification to my script to preserve the last file and delete all.

Anyway i modified the script adding a variable which greps for last file and delete using grep -v of that last file. It was just one more line to my existing script.

Thanks again
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List only the recent log files sudvishw AIX 3 11-11-2009 09:24 AM
script to ftp recent changed files RApds AIX 2 11-04-2009 04:11 AM
Find most recent files in dirs and tar them up? bobdung Shell Programming and Scripting 1 04-06-2009 09:22 AM
sort biggest most recent files tjmannonline UNIX for Dummies Questions & Answers 3 12-16-2008 12:23 AM
To keep only the most recent files *Jess* Shell Programming and Scripting 1 07-26-2007 11:11 PM



All times are GMT -4. The time now is 10:23 AM.