Better to Delete or Overwrite

Hello All,

I had just a question about my Bash Script I'm currently writing.

The script I have writes some text to a output file. After I write to the output file I send the file to another server to do
some stuff with it.

After the file sends in the script, I don't need the output/txt File anymore. So is it better to, after I send the file of course, delete
the file or should I just leave it and overwrite it the next time I run the script?

The script will be run automatically using cron or automatically using remote means from another server. But basically
it will be executed a couple times an hour (*maybe 2 or 3 times)...

So is it "bad" to delete the file from within the script when it has finished executing? I just didn't want the file to be appended
to somehow and then I'd get incorrect data, if ya know what I mean..?

Any thoughts would be much appreciated..!

Thanks in Advance,
Matt

If you have confirmed that the file was transferred successfully to the remote host, there is no need to continue consuming the disk space occupied by the file. It is just good practice to remove temporary files when they're no longer needed.

But, if there is a chance that you'll need to resend the file or look at the data that was sent to the remote host before cron kicks off the next job, keep it.

1 Like

Hey Don, thanks for the reply!

Cool, that's kinda what I was thinking, but i wasn't sure if removing the file a few times an hour would be better or worse
then to just overwrite it each time...

But, that should be fine. I'll set something up at the very end of my script to remove the file after I confirm it was sent.

Thanks Again Don for the reply!

Thanks,
Matt

Creating a guessable tmp file in a public area like /tmp is a security risk.
It is safer to leave such a file, so the next time it is overwritten not created.
There a methods for a safer tmp file, see

man mktemp

Most safe is to avoid a tmp file. E.g. by using | and ( sub shell ) .

Hey MadeInGermany, thanks for the reply.

The file won't be in a public place. It is being saved in it's OWN directory, and no one has access to the directory except for root...

Don't know if that makes a difference in your eyes, but that's how its setup at the moment.

Thanks Again for the reply,
Matt