HomeLinux5 Methods to Empty or Delete a Giant File Content material in...

5 Methods to Empty or Delete a Giant File Content material in Linux


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.

Warning: Earlier than we proceed to have a look at the assorted methods, word that as a result of in Linux every thing is a file, you have to all the time guarantee that the file(s) you might be emptying will not be essential consumer or system information. Clearing the content material of a important system or configuration file may result in a deadly utility/system error or failure.

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
Empty Large File Using Null Redirect in Linux
Empty Giant File Utilizing Null Redirect in Linux

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
Empty Large File Using Linux Commands
Empty Giant File Utilizing Linux Instructions

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
Empty File Using cat Command
Empty File Utilizing cat Command

Subsequent, we are going to use the cp command to clean a file content material as proven.

# cp /dev/null entry.log
Empty File Content Using cp Command
Empty File Content material Utilizing cp Command

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
Empty File Content Using dd Command
Empty File Content material Utilizing dd Command

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
Empty File Using echo Command
Empty File Utilizing echo Command

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
Empty File Using Null Redirect
Empty File Utilizing Null Redirect

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
Truncate File Content in Linux
Truncate File Content material in Linux

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments