Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to clear text that existed in a text file with out opening it ?

I mean for example I have a file as hello.txt with some text data in it & how can I clear the total text in that file with out opening it.

share|improve this question
    
What do you mean by 'opening' a file? In all the answers given so far the file will still be opened for writing by the shell. In the end the shell is just a program like any other. –  Jeff Dec 13 '13 at 16:19
    
@Jeff not using any thing like nano , gedit etc. –  AgentCool Dec 13 '13 at 16:25
    
Possible duplicate of How to clear the contents of a file from the command line? –  Bleeding Fingers Dec 13 '13 at 16:59
    
Hus787 I have not typed from getting there if you think so else I welcome your interest in my question. –  AgentCool Dec 13 '13 at 17:16
    
I agree with Jeff's comment... I too have opinion* that the file stream has to be opened to write to it, whether you use any text editors, or redirection. (*appreciate anybody suggedting links to help with the matter) –  rusty Dec 27 '13 at 19:10

5 Answers 5

up vote 15 down vote accepted

Just open your terminal with CTRL+ALT+Tand type as

 > hello.txt

that's it, your data in that file will be cleared with out opening it even .

Example:

enter image description here

share|improve this answer
2  
Technically the file will be opened. Just not in an editor. –  John Dibling Dec 13 '13 at 16:13
1  
@JohnDibling actually this is redirection, I believe file is not being opened here. In redirection The content of stdout overwrites the existing file if any. In this case there is nothing at stdout. So the file is being overwritten with void, essentially wiping preveous data if any. –  souravc Dec 13 '13 at 17:02

I am also going to use redirection like rajagenupula's answer. But there is a little more flexibility. Open a terminal and type,

cat > hello.txt

And press Ctrl+C. It will wipe out the previous file. If you want upto this much it is fine.

If you wish you can do something more after wiping the file. In this way not only you can wipe a file without opening but also you can write a few lines with proper formatting in the file. Say you wish to write "Ubuntu is the best OS" after wiping the file, just do

cat > hello.txt
Ubuntu is the 
best OS

Then press Ctrl+C. Now the previous file is wiped out. At the same time words are there in two lines as I put them.

See the example:

enter image description here

share|improve this answer
    
hey thank you for answering. so little but work to be done right after that also & but still interesting .+1 –  AgentCool Dec 13 '13 at 10:24
    
> stands for redirection. cat > file will write stdout to file, overwriting any existing file. –  souravc Dec 13 '13 at 17:08

If a file was created with the name hello.txt and was provided with some texts then the below command in terminalctrl+alt+t will remove all the text in the hello.txt file,

echo "" > hello.txt

enter image description here

share|improve this answer
    
Thank you for answering , +1. expand it with some example please so it can be look more nice. –  AgentCool Dec 13 '13 at 10:23

I have to do this all the time with log files. The easiest way I have found is with the following command:

cat /dev/null > hello.txt

This deletes allo of the content of the file, and leaves you with an empty file without having to open it in an editor, select text, any of that stuff. More specifically what it does is to replace the contents of the file with the contents of "/dev/null", or nothing. It's pretty slick, actually.

The only caveat is that the user you are currently logged in as must have write permission to said file.

share|improve this answer
    
Thank you for the answer. Please add an example so that it can look more nicely.+1 –  AgentCool Dec 13 '13 at 17:17

So, I see a lot of redirections being used to answer this ;)

A little different approach with the combo: rm & touch

rm hello.txt && touch hello.txt

(yeah... yet another cheat!)

So with this command combo the file hello.txt wasn't opened and in the end you still have file hello.txt in its place with the contents cleared. Just like you wanted!

share|improve this answer
    
Thank you for answering –  AgentCool Dec 28 '13 at 1:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.