Empty file content on Linux

 
 

Empty file content on Linux can be quite handy when for example you want to empty a small or a very big log file or any other similar files that could take long time to open, select all lines and finally remove these lines. We know is not so complex to go down this path but for sure is time consuming, too many operations we would say for such a simple task. This quick tutorial will show you ten easy ways how to empty a file on Linux without opening or editing files, nothing fancy or advanced stuff but as we have said before quite handy when you need a fast solution to empty a file on Linux. In the examples listed below we will remove the content of a file name called logfile.log by redirecting its content output to null, or by simply using true, cat command, cp command, echo, truncate and even dd command, we have quite a few choices now for the same goal.

Table of Contents

Redirect content to Null
Using True
Using cat or cp commands
Using echo
Using truncate
Using dd

Redirect content to Null

So lets begin our short step by step guide by describing the first option about how to empty a file on Linux, on this first example we will be redirecting the entire content output of logfile.log file to null which basically represents the non-exiting objects path, so lets run that on our terminal window and see what happens:


$ > logfile.log

Quite easy and fast enough, we can say that this first solution is one of the best to use in order to empty your files on Linux but if you don’t like this solution please read on this article as we have a few more option available for you.

Using True

Another approach to empty a file on Linux would be using the true command like show on both examples listed below:


$ : > logfile.log

Or we can use the next command which in essence is similar to the one above:


$ true > logfile.log

Using cat or cp commands

On the next example we can actually redirect the output of our file straight to /dev/null being using cat or cp command which are by default part of the OS, lets try these commands:


$ cat /dev/null > /file/location/logfile.log

We will get the same result as using cat command when we use cp (copy) command, the main idea is to redirect the output as we said before to /dev/null location:


$ cp /dev/null logfile.log

Using echo

Now lets try to achieve the same result but using echo command this time, we will show you three examples how to use echo command in order to empty logfile.log file:


$ echo "" > logfile.log

Second option using echo command is this one:


$ echo > logfile.log

An finally the third option using echo looks like this:


$ echo -n "" > logfile.log

So we can say that echo can be a good candidate when we have to empty one or multiple files, nothing complex so far.

Using truncate

Another option to empty files on Linux OS’s would be to use truncate command that will provide obviously the same results as all above commands that we’ve used, so lets see truncate command in action as well:


$ truncate -s 0 logfile.log

Using dd

Finally dd command can be handy in similar situations when we need to empty a big file:


$ dd if=/dev/null of= logfile.log

That is all, our short tutorial ends here, we have shown ten different ways how to empty a file on Linux.

Video

No video posted for this page.

Screenshots

No screenshots posted for this page.

Source code

No code posted for this page.

About this page

Article
Empty file content on Linux
Author
Category
Published
06/11/2018
Updated
06/11/2018
Tags

Share this page

If you found this page useful please share it with your friends or colleagues.