Welcome to WebmasterWorld Guest from 54.226.213.110

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)

Junior Member

5+ Year Member

joined:Dec 20, 2009
posts: 56
votes: 0


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 June 20, 2010 (gmt 0)

Senior Member from KZ 

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

joined:Jan 10, 2005
posts: 2889
votes: 5


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 June 20, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 20, 2009
posts:56
votes: 0


Thanks lammert, both commands worked perfectly!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members