Sometimes, whereas coping with information in a Linux terminal, you could wish to clear the content material of a file with out essentially opening it utilizing any Linux command line editors. How can this be achieved?
On this article, we are going to undergo a number of other ways of emptying file content material with the assistance of some helpful instructions.
With that stated, under are technique of clearing file content material from the command line.
Necessary: For the aim of this text, we’ve used the file entry.log
within the following examples.
1. Empty File Content material by Redirecting to Null
The best technique to empty or clean a file content material utilizing shell redirect null
(non-existent object) to the file as under:
# > entry.log

2. Empty File Utilizing ‘true’ Command Redirection
Right here we are going to use an emblem :
is a shell built-in command that’s essence equal to the true
command and it may be used as a no-op (no operation).
One other methodology is to redirect the output of :
or true
built-in command to the file like so:
# : > entry.log OR # true > entry.log

3. Empty File Utilizing cat/cp/dd utilities with /dev/null
In Linux, the null
gadget is principally utilized for discarding undesirable output streams of a course of, or else as an acceptable empty file for enter streams. That is usually achieved by a redirection mechanism.
And the /dev/null
gadget file is due to this fact a particular file that writes off (removes) any enter despatched to it or its output is identical as that of an empty file.
Moreover, you may empty the contents of a file by redirecting the output of /dev/null
to it (file) as enter utilizing the cat command:
# cat /dev/null > entry.log

Subsequent, we are going to use the cp command to clean a file content material as proven.
# cp /dev/null entry.log

Within the following dd command, if
means the enter file and of
refers back to the output file.
# dd if=/dev/null of=entry.log

4. Empty File Utilizing echo Command
Right here, you need to use an echo command with an empty string and redirect it to the file as follows:
# echo "" > entry.log OR # echo > entry.log

Word: You need to take into account that an empty string isn’t the identical as null. A string is already an object a lot as it could be empty whereas null merely means the non-existence of an object.
Because of this, once you redirect the out of the echo command above into the file, and think about the file contents utilizing the cat command, is prints an empty line (empty string).
To ship a null output to the file, use the flag -n
which tells echo to not output the trailing newline that results in the empty line produced within the earlier command.
# echo -n "" > entry.log

5. Empty File Utilizing truncate Command
The truncate command helps to shrink or prolong the scale of a file to an outlined measurement.
You’ll be able to make use of it with the -s
possibility that specifies the file measurement. To empty a file content material, use a measurement of 0 (zero) as within the subsequent command:
# truncate -s 0 entry.log

That’s it for now, on this article we’ve lined a number of strategies of clearing or emptying file content material utilizing easy command line utilities and shell redirection mechanisms.
These will not be most likely the one obtainable sensible methods of doing this, so you can even inform us about every other strategies not talked about on this information by way of the suggestions part under.