- How to clear text in a file?
- 9 Answers 9
- Not the shortest answer but.
- Clear everything except first 10,000 bytes
- Linux how to clear content of text file
- How to clear the contents of a file from the command line?
- Linux Command Line (11) Delete Files and Directories
- Different Ways to Empty or Delete a Large File Content in Linux
- Most efficient method to empty the contents of a file
- How to Empty or Delete a Large File Content in Linux?
- /dev/null
- echo
- Using >
- Using truncate
- Using touch
How to clear text in a file?
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.
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.
Hus787 I have not typed from getting there if you think so else I welcome your interest in my question.
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)
9 Answers 9
Just open your terminal with CTRL + ALT + T and type as
that’s it, your data in that file will be cleared with out opening it even .
@souravc John is correct, the file will be open. Shell opens a file for redirection > with flags O_WRITE|O_TRUNC . In fact, this is stated in the bash manual explicitly under Redirecting Output section: «If the file does not exist it is created; if it does exist it is truncated to zero size.»
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.
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 question..here, take +1..
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
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.
hey thank you for answering. so little but work to be done right after that also & but still interesting .+1
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:
$ echo Hello > Hello.txt $ echo World! >> Hello.txt $ cat Hello.txt Hello World! $ truncate -s 0 Hello.txt $ ll Hello.txt -rw-rw-r-- 1 rick rick 0 Mar 20 17:32 Hello.txt
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:
$ truncate -s 10000 Hello.txt
. 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:
- Run the truncate command for a reasonable larger normal size of 10K
- Open the file with your text editor and press End
- Highlight and PgUp to delete the remaining bytes that don’t belong (usually recognizable by ASCII garbage characters).
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 xieerqi:$
Why does this work and how does this work ? The testFile.txt will be opened with O_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 leaving testFile.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 any read() at all. Big plus of this dd version is that it is POSIXly portable. The dd specifications state:
If the seek= expr conversion is not also specified, the output file shall be truncated before the copy begins if an explicit of= file operand is specified, unless conv= notrunc is specified.
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 GNU cp defaults to using rule 3 in the same spec, which is truncating the existing file without changing its type.
Linux how to clear content of text file
In this article, we will learn different methods to empty or delete large file content in the Linux/Unix system by overwriting the existing target file with a newly created empty file. These are the following method to empty or delete a large file content in the Linux/Unix system.
How to clear the contents of a file from the command line?
will do. This will leave you with an empty file filename .
PS: If you need sudo call, please consider to use truncate as answered here.
You can use the user command : truncate
Using > to write the (null) input from echo -n to the file.
Using >> would append the null input to the file (effectively doing nothing but touch ing it).
Linux Commands – Remove All Text After X, In all our examples we’ll focus on a pretty simple use case to remove the file extension of a filename. 2.1. Extracting Characters Using a Given
Linux Command Line (11) Delete Files and Directories
To delete a file, we use the remove command which is rm and then we type the filename we
Duration: 7:41
Different Ways to Empty or Delete a Large File Content in Linux
In this article, we will learn different methods to empty or delete large file content in the Linux/Unix system by overwriting the existing target file with a newly created empty file. Before we proceed with the given various ways, please make sure that the file we’re emptying or deleting content is not important or system file because once the content of the file is deleted by means of the given ways, recovery of the file is not possible.
These are the following method to empty or delete a large file content in the Linux/Unix system.
- Using the truncate command.
- Using the echo command.
- Using the redirection ( >) operator.
- Using the true redirection ( : > ).
- Using /dev/null.
1. Empty or delete the contents of a large file using the truncate command in the Linux/Unix system.
The truncate command is used to shrink or extend the size of a file to a specified size in the Linux system. It is also used to empty large file contents by using the -s option followed by 0 (zero) specified size.
In the below example, we will empty the contents of a large file using the truncate command as shown below.
2. Empty or delete the contents of a large file using the echo command in the Linux/Unix system.
Basically, the echo command is used to display lines of text. It is also used to empty the contents of a large file as shown below example.
3. Empty or delete the contents of a large file using the > (redirection operator) in the Linux/Unix system.
To empty the contents of a large, we can just put > (redirection operator) before a file name as shown below example.
4. Empty or delete the contents of a large file using the :> (true redirection) in the Linux/Unix system.
A symbol : is a shell-built command which is equivalent to a true command. To empty or delete the contents of a large, we can also use :> or true > followed by file name as shown below example.
5. Empty or delete the contents of a large file using the /dev/null device file in the Linux/Unix system.
In Linux/Unix system a /dev/null is a special device file that removes the contents of a file and outputs an empty file. In the below example, we will make empty the contents of a file.
Clear data inside text file in c++, If you simply open the file for writing with the truncate-option, you’ll delete the content. std::ofstream ofs; ofs.open(«test.txt»,
Most efficient method to empty the contents of a file
Actually, the second form touch filename doesn’t delete anything from the file — it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.
cp /dev/null filename works.
As for efficient, the most efficient would be truncate -s 0 filename (see here).
Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null , so that makes it marginally slower.
On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and then run it.
From the man page of echo :
-n Do not print the trailing newline character.
There is a builtin command «:», which is available in sh,csh,bash and others maybe, which can be easily used with the redirecting output operator > truncate a file:
What I like on this is, that it does not need any external commands like «echo» etc.
One big advantage of truncating files instead of deleate/recreate them is, that running applications which works with this file (e.g. someone makes an tail -f filename or a monitoring software, . ) don’t have to reopen it. They just can continue using the filedescriptor and gets all the new data.
Remove the Last N Lines of a File in Linux, For example, let’s delete from line 5 until the end of our input.txt: $ sed ‘5,$ d’ input.txt 01 is my line number. Keep me please! 02 is my
How to Empty or Delete a Large File Content in Linux?
Usually a continuously growing files, needs to be emptied from time to time to accept the latest data from the next operation. There are various mechanisms to empty the file. We will see them one by one below. The common approach is the overwrite the target file by using > sign which should come from a source which is empty.
/dev/null
This is a common method where we output empty result and then redirect that result to the target file.
# Original file size $ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt # Redirect the output from /dev/null $ cat /dev/null > ref_file.txt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:35 ref_file.txt
echo
The echo command with null out put can also make the target file empty.
$ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt $echo > ref_file.txt $ls –lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:36 ref_file.txt
Using >
Just putting only a > just before the file name will make it empty. We can also use just a colon(:) to get the same result.
$ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt $ > ref_file.txt $ls –lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:39 ref_file.txt
Using truncate
Using truncate command with the size option as 0 will make the target file empty.
$ truncate s -0 ref_file.txt $ ls-lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:41 ref_file.txt
Using touch
$ touch ref_file.txt $ ls-lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:45 ref_file.txt
Linux — Removing new lines from text file in order for text to appear on, xargs and printf is also an option: xarg printf «%s» < file >passed.txt # Redirect file into printf and print each default space variable