Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SHELL SCRIPT TO DELETE ALL BUT LATEST file 1

Status
Not open for further replies.

Scunningham99

Programmer
Sep 20, 2001
815
GB
Morning all

I would like to create a script which will delete all files but the latest.

Here is what i have got, but it doesnt seem to be working. Please can someone help me.

kind regards

S


while [ 1 ]
echo $$ > /u00/DBA/scripts/rmArchLogs.txt # write PID to text file

#find the latest archive file
cd /u02/archive/ALIVE_SB/
FILE=`ls -tr *.arc | tail -1`;

do #loop through files to delete
#Delete Files if it does not = the latest file!!
FILE_RM=`ls *.arc`;
if ( $FILE_RM -ne $FILE ); then

echo deleting file ... $FILE_RM

fi;

sleep 300 # sleep for 5 min
done # end loop

Sy UK
 
Its not as simple as the fact that your not doing an 'rm' is it?
 
for the moment i was just using echo to spool to screen all the files in dir that are not equal to the latest file, then replace with rm later.

but it is not even running, it does the following instead:-

./rmArchLogs.sh #this is the name of the script!!

./rmArchLogs.sh[11]: SWPROD_27700.arc: not found

(SWPROD_27700.arc is just one of the files in the dir!)

Sy UK
 

do #loop through files to delete
#Delete Files if it does not = the latest file!!
FILE_RM=`ls *.arc`;
if ( $FILE_RM -ne $FILE ); then

echo deleting file ... $FILE_RM

fi;

There is a logic error in this setup. $FILE_RM will contain a string with '<file1>.arc <fil2e>.arc <file3>.arc ...'

You need to loop through each file, like so:
for F in $FILE_RM ; do
if ($F ne $FILE) ;
then `rm $F`;
fi
; done

Syntax not checked, but I think you get the idea.
 
Shouldn't your () be the test operator []?

I take it the files get updated to often to be able to use a command of the form &quot;find <dir> -name *.arc -mtime +1 -exec rm {} \;&quot; (deletes all files over 1 day old)
 
Thanks for reply

correct to often for find on day!!
these are archive logs on standby oracle db, which do not need to be kept apart from latest..

HERES WHAT I HAVE GOT AT MOMENT:-
while [ 1 ]
echo $$ > /u00/DBA/scripts/rmArchLogs.txt # write PID to text file

#find the latest archive file
cd /u02/archive/ALIVE_SB/
FILE=`ls -tr *.arc | tail -1`;

do #loop through files to delete
#Delete Files if it does not = the latest file!!
FILE_RM=`ls *.arc`;

for F in $FILE_RM ; do
if ($F ne $FILE) ;
#then `rm $F`;
then `deleting... $F`;
fi
done



sleep 300 # sleep for 5 min
done # end loop

BUT WHEN I RUN IT I GET THE FOLLOWING AND I CANNOT UNDERSTAND WHY:--

./rmArchLogs.sh
./rmArchLogs.sh[13]: SWPROD_27700.arc: not found
./rmArchLogs.sh[13]: SWPROD_27701.arc: not found
./rmArchLogs.sh[13]: SWPROD_27702.arc: not found
./rmArchLogs.sh[13]: SWPROD_27703.arc: not found
./rmArchLogs.sh[13]: SWPROD_27704.arc: not found
./rmArchLogs.sh[13]: SWPROD_27705.arc: not found
./rmArchLogs.sh[13]: SWPROD_27706.arc: not found
./rmArchLogs.sh[13]: SWPROD_27707.arc: not found
./rmArchLogs.sh[13]: SWPROD_27708.arc: not found
./rmArchLogs.sh[13]: SWPROD_27709.arc: not found
./rmArchLogs.sh[13]: SWPROD_27710.arc: not found
./rmArchLogs.sh[13]: SWPROD_27711.arc: not found
./rmArchLogs.sh[13]: SWPROD_27712.arc: not found
./rmArchLogs.sh[13]: SWPROD_27713.arc: not found
./rmArchLogs.sh[13]: SWPROD_27714.arc: not found
./rmArchLogs.sh[13]: SWPROD_27715.arc: not found
./rmArchLogs.sh[13]: SWPROD_27716.arc: not found
./rmArchLogs.sh[13]: SWPROD_27717.arc: not found
./rmArchLogs.sh[13]: SWPROD_27718.arc: not found
./rmArchLogs.sh[13]: SWPROD_27719.arc: not found
./rmArchLogs.sh[13]: SWPROD_27720.arc: not found


Sy UK
 
The error line number, 13, points to the if condition.

This should be a test i.e. if [ &quot;$F&quot; != &quot;$FILE&quot; ] ; then ...
not just brackets

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top