Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. – Raja Dec 13 '13 at 16:25
Hus787 I have not typed from getting there if you think so else I welcome your interest in my question. – Raja 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) – precise Dec 27 '13 at 19:10
up vote 47 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 .


enter image description here

share|improve this answer
Technically the file will be opened. Just not in an editor. – John Dibling Dec 13 '13 at 16:13
@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
Is there a way to undo this command? – Rao Nov 9 '15 at 15:09
@Rao , If its a large like >10GB , may be. But how much extent not sure. – Raja Nov 9 '15 at 15:11

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 – Raja 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

The easiest way is to truncate a file is to redirect the output of the shell no-op command (:) to the file you want to erase.

: > hello.txt
share|improve this answer
Its probably not the easiest (relative to just >) but the fastest for sure..i wonder why it has not been mentioned in any of the answers of this highly viewed, take +1.. – heemayl Nov 15 '15 at 1:49

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 – Raja Dec 13 '13 at 17:17

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. – Raja Dec 13 '13 at 10:23

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 – Raja Dec 28 '13 at 1:45
Not a good 'cheat' in my opinion, it can change file permissions... – Parto Aug 11 '15 at 10:50
This might be better of deleted. It can lead to problems: permisions can change. If done on a log file it can lead to unexpected behaviour from the service that expects the log to be there and to be writable. – Rinzwind Nov 15 '15 at 5:49

Another approach - cp the /dev/null to the file

xieerqi:$ cat testFile.txt                                                                                        
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1      115247656 83100492  26269816  76% /
none                   4        0         4   0% /sys/fs/cgroup
udev             2914492        4   2914488   1% /dev
tmpfs             585216     1152    584064   1% /run
none                5120        0      5120   0% /run/lock
none             2926072    98096   2827976   4% /run/shm
none              102400       76    102324   1% /run/user

xieerqi:$ cp /dev/null testFile.txt                                                                               

xieerqi:$ cat testFile.txt

share|improve this answer

Your Answer


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.