How to clear contents of a file without deleting it.

Hi,

I have a script which will use an input.txt file as an input file.
I am providing data to this input file in the script and once the script is executed, I want to clear all the contents of this file as during the second time use of this script, I'll be appending the data in this input file.

So basically I want to know a way using which I can just clear all the contents of the file input.txt and make it a blank file. Like one which is just created with no data in it.
Can this be done.

Thanks in advance!

# first use of script, clears file
./script > filename
# second use of script, appends file
./script >> filename
> filename
or
cat /dev/null > filename
echo "" > filename

'echo' creates a file with one blank line in it. Not empty.

You don't need cat or /dev/null here.

: > filename

Thank's a Ton guys. It really helped me.
Specially to Corona688... that solution is awesome!!!!

UNIX is amazing and equally amazing are all you guys :slight_smile:

This works fine, no need use even nop = : command.

> filename

This method is correct method to clear open log files. Not rm.