cancel
Showing results for 
Search instead for 
Did you mean: 

script to remove files each day

bob the spod
Occasional Advisor

script to remove files each day

Everyday I have to clear about 10 files from a directory. the files are always prefixed with four letters (always the same) then 6 numbers been the date sent to the directory. this is followed by .REP
so they look like this TEST280203.REP

I intend to write a script to add to a contrab which will remove these for me.
at the moment I have to keep typing...
rm TEST280203.REP ABCD280203.REP DING280203.REP and so on!

can anyone suggest a solution please.

Thanks

bob
you make me feel like dancing (gonna dance the night away!)
11 REPLIES
Pete Randall
Outstanding Contributor

Re: script to remove files each day

Bob,

A simple find command should probably do it. Something along these lines:

01 01 * * 01 /usr/bin/find /tmp -type f -name TEST* -mtime +10 -exec rm {} \;


You can modify the name parameter to suit your needs (like *.REP or whatever) and the mtime (last modifed time) may or may not be useful for you. And, of course, you should change the /tmp to the location you need.

Pete

Pete
bob the spod
Occasional Advisor

Re: script to remove files each day

thanks peter
if I didnt want to search all directories (i'm presuming thats what /usr/bin/find does)
what would you suggest.
I only want my script to point at one directory.
toptip about modifying the time
(im very new to all of this could you break down what each part does for me please)
thanks again
bob
you make me feel like dancing (gonna dance the night away!)
Robin Wakefield
Honored Contributor

Re: script to remove files each day

Hi Bob,

% cd yourdirectory
% ls | grep -E '^[A-Z]{4}[0-9]{6}\.REP' |xargs rm

the regular expression matches:

4 A-Z characters followed by:
6 numbers followed by:
.REP

or using classes:

% ls | grep -E '^[[:alpha:]]{4}[[:digit:]]{6}\.REP' |xargs rm

rgds, Robin
Pete Randall
Outstanding Contributor

Re: script to remove files each day

Bob,

In the example I gave you, just change /tmp to the name of the directory you want to start in, like this:


find /home/bob -type f -name *.REP -exec rm {} \;

The "01 01 * * 01" is the syntax for crontab which says at 1 minute after 1AM, on any day of the month, in any month, on the first day of the week (Monday). The rest of it translates thusly: "/home/bob" is the starting point for the search. "-type f" says we're just looking for files. "-name *.REP" says the file needs to be named anything as long as the name ends in "REP". "-exec rm {} \;" says remove it.


Pete

Pete
Dave La Mar
Honored Contributor

Re: script to remove files each day

Bob -
Attached is a small script used to clean up our /tmp directory daily.
This can be modified for your purposes of rhe .REP files only.

Best regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
bob the spod
Occasional Advisor

Re: script to remove files each day

Pete
I like your last option v much indeed and am going to use it
However I cannot use the *.REP part of it due to some files which I receive are required.

if I specify the front part of the filename would that work (like this)

find /tmp/bob -type f -name ABCD* FACE* HIDE* FOUR* GOLF* POLO* WORD* -exec rm {} \;


would the above remove files like the below
ABCD280203.REP
FACE280203.REP
HIDE280203.REP
FOUR280203.REP
GOLF280203.REP

or is this unlikely to work?
you make me feel like dancing (gonna dance the night away!)
Pete Randall
Outstanding Contributor

Re: script to remove files each day

Bob,

find /tmp/bob -type f \( -name ABCD* -o -name ABCD* -o -name FACE* -o -name HIDE* -o -name FOUR* -o -name GOLF* -o -name POLO* -o -name WORD* \) -exec rm {} \;


That should do it! Test it by substituting an ls command in place of the rm:

find /tmp/bob -type f \( -name ABCD* -o -name ABCD* -o -name FACE* -o -name HIDE* -o -name FOUR* -o -name GOLF* -o -name POLO* -o -name WORD* \) -exec ls {} \;

Pete

Pete
bob the spod
Occasional Advisor

Re: script to remove files each day

pete
Ive tried that exact code below, where will the list appear???

10 16 * * * find /stage/intray -type f \( -name FUPD* -o -name TSFA* -o -name ESFA* -o -name FCOV* -o -name FCMV* -o -na
me FCGV* -o -name FCVV* \) -exec ls {} \;
thanks
bob
you make me feel like dancing (gonna dance the night away!)
Pete Randall
Outstanding Contributor

Re: script to remove files each day

Bob,

For testing purposes, don't submit it to cron, just do the find from the command line. When you do use cron the output is emailed to the submitting user.

Pete

Pete
A. Clay Stephenson
Acclaimed Contributor

Re: script to remove files each day

The problem is that you are not quoting your -name arguments. ABCD* should be 'ABCD*'. You want find to do the expansion NOT the shell.

If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: script to remove files each day

Thanks, Clay! The quotes got lost somewhere in the cut and paste process and I never even noticed.

Pete

Pete