rm files in a directory, looping, counting, then exit

I am trying to write a script that will look for a file in a directory, then remove it. I need it to loop until it has removed a certain number of files. Is it better to do a repeat or to list each file in a pattern? Files will be numbered like RAF.01., RAF.02., etc. Thanks, James

Need a bit more info here James,

Are you looking to remove all instances of files that follow a certain pattern (i.e. RAF.0*)...or are you looking to remove only some of the files that follow the pattern? Expand on what you mean by a certain number of files.....

Also are all the files going to be in the same directory?

If you want to remove from one directory....and all files that are RAF.0*....then you don't need a script you can just use

 rm /path/RAF.0* 

If you can give us a bit more info....that would help./.

Thanks Peter. All the files are in the same directory and I will need to delete all files like RAF.* ... but here is the reason i need a script. The files are being created and placed in an archive directory. The files are so big that it causes the machine to run out of space. I need to run the script in the background, so that when each file is created, it removes it. The code that creates the files cannot be changed because the files are needed in the production environment and i am testing in a separate environment where the files are not needed. Thanks again for your help.

James

What about a cronjob?

Just enter a line like

0,10,20,30,40,50 * * * * rm /PATH/RAF.*

and every 10 minutes the RAF.* will be deleted.

cronjob would be the perfect solution, but my Operations Architecture team owns cronjob and they will not approve that request. I am given all the responsibility with none of the power to get it done.

Thanks anyway isacs

Well hating to suggest a hack method...I will....

You could write a script that continually runs and performs the basic function of a crontab..... i.e.

#!/usr/bin/ksh

while(1)
do
rm /path/RAF.* 2> /dev/null
sleep 600
done

This would run every 600 seconds and remove any files matching the criteria. The Std Error would be thrown away incase there are no files to delete.

You'd need to start this fiel with nohup...so that it ran in the background and wasn't reliant on having a session open. i.e.

nohup script_name &

The issues with this approach are that if you want to stop the process then you'll need to find the PID and kill it. Yuo can do this with:

ps -ef | grep script_name then look for the PID column typically second column. Then

kill 12345 (where 12345 is the process id PID).

The other issue is if the server is shut down - then the script will stop running.

This is NOT the best way and has a number of shortcomings, but if you can't get crontab then maybe it's all you have.

You should enquire about having your own crontab..... in Sun (don't know about HP) it is possible to get crontabs for different users - so you don't have to be root! This would be MUCH more preferable than using this method.

Thanks Peter, I will try that.

James