How to clear text that existed in a text file without opening it?
I mean for example I have a file as hello.txt
with some text data in it, and how can I clear the total text in that file without opening it?
By this, I mean not using any editor like nano, Gedit, etc.
Just open your terminal with CTRL+ALT+Tand type as
that's it, your data in that file will be cleared with out opening it even .
Example:
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.I have to do this all the time with log files. The easiest way I have found is with the following command:
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.
I am also going to use redirection like rajagenupula's answer. But there is a little more flexibility. Open a terminal and type,
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
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:
Another approach -
cp
the/dev/null
to the fileWhy does this work and how does this work ? The
testFile.txt
will be opened withO_WRONLY|O_TRUNC
flags, which means if the file exists - it will be truncated, which means contents discarded and size set to zero. This is the same flag with which>
operator in shell opens the file on the right of that operator.Next,
cp
will attempt to read from/dev/null
and after reading 0 bytes will simply close both files, thus leavingtestFile.txt
truncated and contents effectively deleted.Knowing that, we could in theory use anything that allows us to open a file with
O_TRUNC
. For instance this:Small difference here is that
dd
won't perform anyread()
at all. Big plus of thisdd
version is that it is POSIXly portable. The dd specifications state:By contrast
cp /dev/null testFile.txt
isn't necessarily portable, since POSIX specifications for cp cover what happens only if source_file is non-regular and when-r
/-R
flags are specified (big thanks to Stephen Kitt for pointing this out), but not what happens when-r
or-R
are omitted, which is the case here. However it appears at least GNUcp
defaults to using rule 3 in the same spec, which is truncating the existing file without changing its type.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,
Not the shortest answer but...
This answer is based on another from Super User. Although not the shortest bash command,
truncate
is the most readable for average newbies:Parameters used with
truncate
command here:Clear everything except first 10,000 bytes
An advantage of
truncate
is you can specify how much to keep, not just zero:... will truncate everything after the first 10,000 bytes. This could be useful if a program went crazy and dumped many Megabytes of data into a small log file:
truncate
command for a reasonable larger normal size of 10Kworked the best for me because of the particular permission it had
So, I see a lot of redirections being used to answer this ;)
A little different approach with the combo: rm & touch
(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!