• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2111
  • Last Modified:

how to overwrite an existing text file in shell script ?

I would like to know how to overwrite an existing text file
as i wrote the script likethis ....>> /home/abc.txt.
However, the text file keeps appending texts without overwrite the exsting content at all.  According to what I read from the various sources,
it stated it should use > instead of >>. I tried it but it failed to execute.
Please help on this problem, thanks!
0
cat21
Asked:
cat21
1 Solution
 
girish_nairCommented:
Simply if you use
echo "hello" > /your_dir/abc.txt
On a command prompt it works fine.

HTH

0
 
veerunsCommented:
use
>| to force overwriting
hope it helps
Ram
0
 
DracoXCommented:
In addition to using a single > when trying to write the file, make sure you have permission to write to the directory itself, and not just the file.

0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
veerunsCommented:
ohh yeah right..of course..i thgt that was understood
0
 
GP1628Commented:
in cases where you only have access to the file and not the directory, you can "empty out" the file by doing..

cp /dev/null /home/abc.txt

then the >> can append to the newly zero'd file.

Gandalf  Parker
0
 
veerunsCommented:
yeah that would be a really good idea
0
 
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: GP1628 {http:#8251333}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now