1834253 Members
2117 Online
110066 Solutions
New Discussion

Script

 
SOLVED
Go to solution
Ali Imran Abbas
Regular Advisor

Script

On my server a file is created every morning at 10 0' clock. And whenever the new file is created, it deletes the old one. I wanna keep these files for a week and then delete them by the end of the week. Can any one help me to make a script to do this job, please?
5 REPLIES 5
Mridul Shrivastava
Honored Contributor
Solution

Re: Script

Which file you are talking about?

Is there a standard naming convention for the file name? you can simply add a cron entry to copy that file to another location before the deletion time so you will have a copy of that.
Time has a wonderful way of weeding out the trivial
Pete Randall
Outstanding Contributor

Re: Script

Set up a cron job that runs the find command. You could do it daily or weekly, whichever works for you, and simply search for the file name with a last modification time over one week old:

find /start-dir -type f -name your_file_name -mtime +7 -exec rm {} \;

- or -

find /start-dir -type f -name your_file_name -mtime +7 |xargs rm

The second version is probably a bit more efficient.


Pete

Pete
Laurent Menase
Honored Contributor

Re: Script

typeset -i i=6

while [[ i!=0 ]]
do
if [ -f myfile.$i ]
then
mv myfile.$i myfile.$((i+1))
fi
mv newfile myfile.0
done





Jeeshan
Honored Contributor

Re: Script

you can try out this

find . -type f ! -mtime -7 -exec rm {} \;
a warrior never quits
Arturo Galbiati
Esteemed Contributor

Re: Script

Hi,

1) in your crontab script you already have add this at the beginning:
cp your_file your_file _$(date +%u)

this will copy your file adding at the end the number of the day of the week 9see man date for further info)

2) create a new script scheduled the last day of the week:
rm your_file_

last day of the week depedn on your environment usually is 6.

Just my .02$
Rgds,
Art