Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm doing a shell command (such as sh run.sh > log.txt). How do you overwrite the file each time (so each time I run it log.txt doesn't get appended)

I'm doing this in a python script and I'm checking the log file each time for a specific number (and I don't want the numbers to get added onto the log.txt, just replaced)

share|improve this question
up vote 4 down vote accepted

The > operator DOES overwrite the file by first truncating it to be empty and then writing. The >> operator would append. Perhaps you are actually using that?

share|improve this answer
ya your right.... apparently I was using the wrong one in the script. My bad – user4877 May 18 '11 at 16:25
And if you want your existing files to be protected against using > by mistake instead of >>, you can put set -o noclobber in your .bashrc – rozcietrzewiacz Aug 6 '11 at 21:36
@rozcietrzewiacz: True, but that's a little bit dangerous in the same way that aliasing rm to some kind of trash command is: it sets you up with a safety net that creates bad habits that get you burned when the net is gone. – Caleb Aug 6 '11 at 21:41
I understand your point, but cannot entirely agree. Yes, not paying attention to what you type is a bad habit. But mistakes happen to everyone - it is human. So maybe the best way would be to first learn things the hard way (no safety nets) and then open some nets, where the results of mistakes can be dramatic. But this is a matter of individual philosophy, of course. – rozcietrzewiacz Aug 6 '11 at 21:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.