homepage Welcome to WebmasterWorld Guest from 54.226.182.192
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

    
Shell - Clear Text File
need a shell script to clear the contents of a text file
cookiemonster




msg:4155334
 7:46 pm on Jun 19, 2010 (gmt 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!

 

lammert




msg:4155586
 3:13 pm on Jun 20, 2010 (gmt 0)

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 {} \;

cookiemonster




msg:4155592
 3:29 pm on Jun 20, 2010 (gmt 0)

Thanks lammert, both commands worked perfectly!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved