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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Force File Overwrite

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
US
I've got a KSH script that overwrites a couple existing files. When I run it manually, I receive the question "are you sure you want to overwrite file?"

mv $DATA_DIR/file_A.dat $DATA_DIR/file_A.$day

When I set this job in cron, it changes the filename to "file_A." because the filename with today's day abbreviation already exists and it won't overwrite it. I've got chmod = 775, but what command can I set to force the overwrite and what's the syntax?
 
In the man page of the mv command have a look at the -f option.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Gotcha...so basically,

mv -f $DATA_DIR/file_A.dat $DATA_DIR/file_A.$day

should do the trick, right?
 
it changes the filename to "file_A."
Are you sure that $day holds a value in your cron job ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes: day=`date +"%a" | tr '[A-Z]' '[a-z]'`

It's a new cron and it worked all last week just fine. As soon as it ran this morning and found a "fileA.mon" in the data directory, it didn't overwrite it.
 
You may have an alias defined for [tt]mv[/tt]. The "[tt]-i[/tt]" option of the [tt]mv[/tt] command causes it to ask you first if the [tt]mv[/tt] would overwrite a file. My guess is that there's an alias defined like "[tt]mv='mv -i'[/tt]".

Type "[tt]alias[/tt]" to see if you have it defined.

See the [tt]man[/tt] page for [tt]mv[/tt] for more info.
 
Why not expand your date extension to include a ddmmyy construct, thus avoiding the need to overwrite. You can then remove any older files at your leisure and you have a historical record of events. Just a thought.
 
And what happened last night ?
I think there already were files fileA.mon, fileA.tue and fileA.
My guess is, it renamed it to fileA. , in spite of the existing fileA. ?
[wink]
 
Actually, my cron worked well this morning. It overwrote the existing fileA.tue as designed.

Ken, to answer your question, I actually do have that kind of convention going on for some of my log files:

`date +"%y"`
`date +"%m"`

in the filename with:
find $LOG_DIR -name "fileA*.log" -mtime +35 -exec rm {} \;

to delete after 35 days. But for the data files, we really only need to keep 7 days' worth, so the day extension works OK.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top