The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do you list the most recent files writen to a volume williamhsman OS X (Apple) 1 10-12-2007 09:19 AM
Get Files from ftp which are uploaded recent week narramadan Shell Programming and Scripting 1 09-27-2007 05:05 AM
To keep only the most recent files *Jess* Shell Programming and Scripting 1 07-26-2007 08:11 PM
when I try to run rm on multiple files I have problem to delete files with space umen UNIX for Dummies Questions & Answers 1 09-20-2005 12:20 AM
I can't delete some files DISTURBED UNIX for Dummies Questions & Answers 4 08-01-2002 08:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-21-2005
Registered User
 

Join Date: Aug 2001
Location: Toronto
Posts: 29
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
Reply With Quote
Forum Sponsor
  #2  
Old 06-21-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
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
Reply With Quote
  #3  
Old 06-21-2005
Just Ice's Avatar
Lights on, brain off.
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 629
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
Reply With Quote
  #4  
Old 06-21-2005
Registered User
 

Join Date: Oct 2002
Posts: 676
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?
Reply With Quote
  #5  
Old 06-21-2005
Just Ice's Avatar
Lights on, brain off.
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 629
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 ...
Reply With Quote
  #6  
Old 06-21-2005
Registered User
 

Join Date: Aug 2001
Location: Toronto
Posts: 29
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0