Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: bakedjake

Message Too Old, No Replies

Shell - Clear Text File

need a shell script to clear the contents of a text file

   
7:46 pm on Jun 19, 2010 (gmt 0)

5+ Year Member



Hi,

I need a simple script that can clear the contents of a text (.txt) file without actually DELETING the file.
Is there any way this can be done?
Also, is there any way this can be done to clear the contents of all files with the name "hits.txt" in a given directory?

Thanks!
3:13 pm on Jun 20, 2010 (gmt 0)

WebmasterWorld Senior Member lammert is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The easiest way to clear one file is to copy the null device to the text file:

/bin/cp /dev/null /somedirectory/hits.txt

For all files in the /start/directory tree with the name hits.txt, the following script might work:

/usr/bin/find /start/directory -type f -name hits.txt -exec /bin/cp /dev/null {} \;
3:29 pm on Jun 20, 2010 (gmt 0)

5+ Year Member



Thanks lammert, both commands worked perfectly!